#creating maven plugin

1 messages ยท Page 1 of 1 (latest)

winged bone
ashen jettyBOT
#

โŒ› This post has been reserved for your question.

Hey @winged bone! 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.

shy nebula
#

the latest should be 1.5

winged bone
#

๐Ÿ‘

shy nebula
#

and you can probably also configure more

winged bone
#

now it also created a test class that fails...

shy nebula
#

Did you run mvn verify before making any changes?

winged bone
#
[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

shy nebula
#

What exactly did you run and where?

#

and how did you run it?

winged bone
#

i ran verify from here

#

i dont think i have maven installed outisde of intelliJ

shy nebula
#

Can you show the full output?

winged bone
shy nebula
#

Is there an mvnw file?

winged bone
#

nope

shy nebula
#

Can you show the pom.xml?

winged bone
shy nebula
#

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

winged bone
#

oh ok

shy nebula
#

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

winged bone
#

"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

shy nebula
#

What's the exact wording?

winged bone
#

its in italian ๐Ÿ˜…

+ ... 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"

shy nebula
#

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

winged bone
#

ooh ok it ran but theres no visible output in the terminal

shy nebula
#

Alternatively it might not be mvn but mvn.bat or mvn.cmd - try using autocomplete

winged bone
#

The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program.

winged bone
shy nebula
shy nebula
#

Can you press the ctrl key twice in IntelliJ?

#

that should pop up a text field I think

winged bone
#

yep

shy nebula
#

try entering mvn verify -X there

#

maybe that works

winged bone
#

You need to specify a goalPrefix as it can not be correctly computed
this is what causes the error

shy nebula
#

sounds like your plugin misses some configuration

#

add this to the <configuration> of the maven-plugin-plugin:

<goalPrefix>YOUR_PLUGIN_PREFIX</goalPrefix>
winged bone
shy nebula
shy nebula
#

once the CLI build works, you can reload the Maven project in IntelliJ and it should realise the plugin exists

winged bone
#

verify works!

winged bone
shy nebula
winged bone
#
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
                }
            }
        }
    }
}
shy nebula
# winged bone verify works!

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

winged bone
shy nebula
#

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)

winged bone
#
[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>
winged bone
#

both tests passed

#

ooh its the assertion that failed

shy nebula
#

yep

#

Is there a rouch.txt anywhere?

#

touch.txt*

#

probably in the target directory of the integration test project

#

or something like that

winged bone
#

no there isnt :I

shy nebula
#

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?

winged bone
#

i created one insrc/it/simple-it/touch.txt and it did get pasted in the target

shy nebula
#

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

winged bone
#

nope

#

should i make one and put touch.txt in there?

shy nebula
#

Are there any other target directories?

winged bone
#

there's this one but its outside src

shy nebula
#

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 :)

shy nebula
#

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!

winged bone
winged bone
shy nebula
#

that should delete all the output files first

winged bone
#

it says it cant find the file

shy nebula
#

Can you select your main project pom.xml and run that command again?

winged bone
#

oh ok now it runs

#

same error from test fail as before tho, no file found in target/..

shy nebula
#

Now what exactly is in target?

#

specifically target/its/simple-it/target

winged bone
#

theres no target/its/simple-it/target folder

#

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

shy nebula
#

you could running mvn install and then trying again

#

good night

winged bone
#

thanks โค๏ธ