creating maven plugin

No description
66 Replies
JavaBot
JavaBot4mo ago
This post has been reserved for your question.
Hey @asdru! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Asdru
AsdruOP4mo ago
👍
dan1st
dan1st4mo ago
and you can probably also configure more
Asdru
AsdruOP4mo ago
now it also created a test class that fails...
No description
dan1st
dan1st4mo ago
Did you run mvn verify before making any changes?
Asdru
AsdruOP4mo ago
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.13.1:helpmojo (help-goal) on project maven-plugin-test: You need to specify a goalPrefix as it can not be correctly computed -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.13.1:helpmojo (help-goal) on project maven-plugin-test: You need to specify a goalPrefix as it can not be correctly computed -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
oop
dan1st
dan1st4mo ago
What exactly did you run and where? and how did you run it?
Asdru
AsdruOP4mo ago
i ran verify from here
No description
Asdru
AsdruOP4mo ago
i dont think i have maven installed outisde of intelliJ
dan1st
dan1st4mo ago
Can you show the full output?
Asdru
AsdruOP4mo ago
dan1st
dan1st4mo ago
Is there an mvnw file?
Asdru
AsdruOP4mo ago
nope
dan1st
dan1st4mo ago
Can you show the pom.xml?
Asdru
AsdruOP4mo ago
<version>3.7.0</version> at the bottom seems to throw an error
dan1st
dan1st4mo ago
Can you run mvn test? It is possible that it fails because you are running it from IntelliJ since the integration tests are running Maven on other projects to check the plugin working correctly
Asdru
AsdruOP4mo ago
oh ok
Asdru
AsdruOP4mo ago
dan1st
dan1st4mo ago
Can you run C:\Program Files\JetBrains\IntelliJ IDEA 2024.2.3\plugins\maven\lib\maven3\bin\mvn verify -X? or however you run programs with spaces in the path on Windows
Asdru
AsdruOP4mo ago
"C:\Program Files\JetBrains\IntelliJ IDEA 2024.2.3\plugins\maven\lib\maven3\bin\mvn" verify -X should be like this and it says that it doesnt recognize verify
dan1st
dan1st4mo ago
What's the exact wording?
Asdru
AsdruOP4mo ago
its in italian 😅
In riga:1 car:86
+ ... ns\IntelliJ IDEA 2024.2.3\plugins\maven\lib\maven3\bin\mvn" verify -X
+ ~~~~~~
Token 'verify' imprevisto nell'espressione o nell'istruzione.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
In riga:1 car:86
+ ... ns\IntelliJ IDEA 2024.2.3\plugins\maven\lib\maven3\bin\mvn" verify -X
+ ~~~~~~
Token 'verify' imprevisto nell'espressione o nell'istruzione.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
"Token 'verify' unexpected in the expression or instruction"
dan1st
dan1st4mo ago
ok that's a PowerShell error Can you add a & at the beginning of the command? so & "C:\..." I don't use Windows though lol and especially not PowerShell
Asdru
AsdruOP4mo ago
ooh ok it ran but theres no visible output in the terminal
dan1st
dan1st4mo ago
Can you try that one in cmd instead of PowerShell? Alternatively it might not be mvn but mvn.bat or mvn.cmd - try using autocomplete
Asdru
AsdruOP4mo ago
The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program. from the powershell
dan1st
dan1st4mo ago
oh you don't even have a Java installation configuted outside of IntelliJ that means you were able to run Maven but it wanted a Java installation Can you press the ctrl key twice in IntelliJ? that should pop up a text field I think
Asdru
AsdruOP4mo ago
yep
dan1st
dan1st4mo ago
try entering mvn verify -X there maybe that works
Asdru
AsdruOP4mo ago
this is the full log
Asdru
AsdruOP4mo ago
You need to specify a goalPrefix as it can not be correctly computed this is what causes the error
dan1st
dan1st4mo ago
sounds like your plugin misses some configuration add this to the <configuration> of the maven-plugin-plugin:
<goalPrefix>YOUR_PLUGIN_PREFIX</goalPrefix>
<goalPrefix>YOUR_PLUGIN_PREFIX</goalPrefix>
Asdru
AsdruOP4mo ago
idk if this causes some of the errors but i copied the pom acactly from here https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-invoker-plugin/3.9.0
No description
dan1st
dan1st4mo ago
with YOUR_PLUGIN_PREFIX being replaced by the short nane of your plugin - goals would then be invoked with mvn YOUR_PLUGIN_PREFIX:YOUR_GOAL_NAME that's just IntelliJ lying to you and it's kinda normal once the CLI build works, you can reload the Maven project in IntelliJ and it should realise the plugin exists
Asdru
AsdruOP4mo ago
verify works!
No description
Asdru
AsdruOP4mo ago
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.13.1</version>
<configuration>
<goalPrefix>test</goalPrefix>
</configuration>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.13.1</version>
<configuration>
<goalPrefix>test</goalPrefix>
</configuration>
</plugin>
so mvn com.ale:test ?
dan1st
dan1st4mo ago
Can you show your mojo class?
Asdru
AsdruOP4mo ago
package com.ale;


import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Goal which touches a timestamp file.
*/
@Mojo( name = "touch", defaultPhase = LifecyclePhase.PROCESS_SOURCES )
public class MyMojo
extends AbstractMojo
{
/**
* Location of the file.
*/
@Parameter( defaultValue = "${project.build.directory}", property = "outputDir", required = true )
private File outputDirectory;

public void execute()
throws MojoExecutionException
{
File f = outputDirectory;

if ( !f.exists() )
{
f.mkdirs();
}

File touch = new File( f, "touch.txt" );

FileWriter w = null;
try
{
w = new FileWriter( touch );

w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
finally
{
if ( w != null )
{
try
{
w.close();
}
catch ( IOException e )
{
// ignore
}
}
}
}
}
package com.ale;


import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Goal which touches a timestamp file.
*/
@Mojo( name = "touch", defaultPhase = LifecyclePhase.PROCESS_SOURCES )
public class MyMojo
extends AbstractMojo
{
/**
* Location of the file.
*/
@Parameter( defaultValue = "${project.build.directory}", property = "outputDir", required = true )
private File outputDirectory;

public void execute()
throws MojoExecutionException
{
File f = outputDirectory;

if ( !f.exists() )
{
f.mkdirs();
}

File touch = new File( f, "touch.txt" );

FileWriter w = null;
try
{
w = new FileWriter( touch );

w.write( "touch.txt" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating file " + touch, e );
}
finally
{
if ( w != null )
{
try
{
w.close();
}
catch ( IOException e )
{
// ignore
}
}
}
}
}
dan1st
dan1st4mo ago
then you can reload the Maven project in IntelliJ to get rid of the visual error/IntelliJ lying about the plugin not existing - if you still have that
Asdru
AsdruOP4mo ago
hmm i reloaded but its still there
dan1st
dan1st4mo ago
your goalPrefix is test and your mojo name is touch so you could run mvn test:touch in a project using the plugin mvn -Prun-its verify -Prun-its activates the run-its profile which should force the invoker plugin to be downloaded (and it should also run the integration tests for the plugin which is how you'd test the plugin)
Asdru
AsdruOP4mo ago
[ERROR] The following builds failed:
[ERROR] * simple-it\pom.xml
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.486 s
[INFO] Finished at: 2025-05-24T00:55:32+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-invoker-plugin:3.9.0:verify (integration-test) on project maven-plugin-test: 1 build failed. See console output above for details. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] The following builds failed:
[ERROR] * simple-it\pom.xml
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.486 s
[INFO] Finished at: 2025-05-24T00:55:32+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-invoker-plugin:3.9.0:verify (integration-test) on project maven-plugin-test: 1 build failed. See console output above for details. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
tho it did remove the error from the other pom which is nice :) the pom that's throwing the error is this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ale.it</groupId>
<artifactId>simple-it</artifactId>
<version>1.0-SNAPSHOT</version>

<description>A simple IT verifying the basic use case.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>touch</id>
<phase>validate</phase>
<goals>
<goal>touch</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ale.it</groupId>
<artifactId>simple-it</artifactId>
<version>1.0-SNAPSHOT</version>

<description>A simple IT verifying the basic use case.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>touch</id>
<phase>validate</phase>
<goals>
<goal>touch</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
dan1st
dan1st4mo ago
no output before that?
Asdru
AsdruOP4mo ago
this is the full output
Asdru
AsdruOP4mo ago
both tests passed ooh its the assertion that failed
dan1st
dan1st4mo ago
yep Is there a rouch.txt anywhere? touch.txt* probably in the target directory of the integration test project or something like that
Asdru
AsdruOP4mo ago
no there isnt :I
dan1st
dan1st4mo ago
maybe in target of the main project there's a directory for the test with another target directory Is there a simple-it\target directory?
Asdru
AsdruOP4mo ago
i created one insrc/it/simple-it/touch.txt and it did get pasted in the target
dan1st
dan1st4mo ago
I mean the plugin is supposed to create that file but in a different location Is there really no src/it/simple-it/target directory? Check in the Windows explorer, not just in IntelliJ
Asdru
AsdruOP4mo ago
nope should i make one and put touch.txt in there?
dan1st
dan1st4mo ago
Are there any other target directories? no
Asdru
AsdruOP4mo ago
there's this one but its outside src
No description
dan1st
dan1st4mo ago
The purpose of the sample plugin is to create that file and it seems like it doesn't - your plugin would probably do something else :) ok yes if the touch.txt wasn't created by you, it means the plugin worked but idk why the test didn't work lol So you have a working plugin - but the test seems to have some issues If in doubt, blame someone/something else - so I blame ... Windows!
Asdru
AsdruOP4mo ago
no that was generated by maven when i put the touch.txt here i deleted this and reran mvn -Prun-its verify and now it doesnt get generated
dan1st
dan1st4mo ago
Can you run mvn -Prun-its clean verify? that should delete all the output files first
Asdru
AsdruOP4mo ago
it says it cant find the file
No description
dan1st
dan1st4mo ago
Can you select your main project pom.xml and run that command again?
Asdru
AsdruOP4mo ago
oh ok now it runs same error from test fail as before tho, no file found in target/..
dan1st
dan1st4mo ago
Now what exactly is in target? specifically target/its/simple-it/target
Asdru
AsdruOP4mo ago
theres no target/its/simple-it/target folder
No description
Asdru
AsdruOP4mo ago
ughh i need to go to sleep... i'll look for another approach tomorrow or maybe even how to make a gradle plugin. I appreciate the help so far tho, thank you very much
dan1st
dan1st4mo ago
you could running mvn install and then trying again good night
Asdru
AsdruOP4mo ago
thanks ❤️
JavaBot
JavaBot4mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?