I have this multi module maven project, with one parent pom and many child poms.
when running maven install, my last module uses a maven exec plugin and runs a main method of some class. The thing is, it seems like the build is stopped once the main method finishes running.
This is my setup:
from my parent pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skip>true</skip>
<executable>java</executable>
</configuration>
</plugin>
from my child pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.example.MainClass</mainClass>
<async>true</async>
<keepAlive>true</keepAlive>
</configuration>
</execution>
</executions>
</plugin>
I tried incorporating
<async>true</async>
<keepAlive>true</keepAlive>
but it still stops after the main method is executed. I do not see BUILD SUCCESS in the end...
The solution was to use exec:exec and not exec:java, because exec:java and maven build run on the same process while exec:exec runs on a different process.
Related
When I run a build with maven using the exec-maven-plugin, it runs everything twice for some reason. Is there a way to fix this so it only runs once? I've tried setting my phase in the pom.xml to compile and package and either way, it runs twice. My pom looks like
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.0</version>
<executions>
<execution>
<id>foo</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>bash</executable>
<commandlineArgs>myscript.sh</commandlineArgs>
</configuration>
</plugin>
</plugins>
</build>
It turned out that adding the phase tag caused the command to get executed twice. Leaving that out, it is now getting run once as expected. I guess it doesn't matter what phase I give it now, it'll always run the goal, which works for me.
If you need to run this early in the build, excluding the phase isn't an option.
You can do something like this instead in the plugin config:
<executions>
<execution>
<id>default</id>
<phase>none</phase> <!-- disable the default execution in validate phase -->
</execution>
<execution>
<id>exec-do-something</id>
<goals>
<goal>java</goal>
</goals>
<phase>generate-sources</phase><!-- now it will run once but in an earlier phase -->
</execution>
</executions>
I saw this happening due to the inclusion of:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This seems to be that the maven-source-plugin causes a re-execution of the generate-sources phase. See https://maven.apache.org/plugins/maven-source-plugin/jar-mojo.html
Invokes the execution of the lifecycle phase generate-sources prior to executing itself.
If I removed this plugin, the exec goal only executed once.
I'm trying to run unit tests on a Grails app and they happen to run during the verify phase of our maven life cycle as opposed to the test phase (To be clear the maven life cycle goes as: "clean"->"validate"->"compile"->"test"->"package"->"verify"->"install"->"site"->"deploy"). Is there any way to re-associate the plugins responsible for testing (i.e. grails:test-app) to the "test" phase?
If you run grails tests from execution plugin, you can specify phase:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<id>Unit-test-app</id>
<phase>test</phase>
<configuration>
<executable>grails</executable>
<commandlineArgs>test-app -unit --non-interactive </commandlineArgs>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I just tried to configure the maven jarsigner plugin for signing a jar project.
As far as I can understand, the plugin should run automatically when I run mvn clean package but it doesn't.
I must run mvn clean package jarsigner:sign for the plugin to be executed.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<configuration>
<executions>
<execution>
<id>sign</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<alias>java-code</alias>
<keystore>mykeystore.keystore</keystore>
<keypass>mykeypass</keypass>
<storepass>mystorepass</storepass>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</build>
I could find the error on my own. The configuration element has to be within the execution element.
Thanks.
Is there any phase I can use to prevent Maven from running a plugin goal or any other way I can tell Maven to skip a plugin goal?
Basically, I want to just run it manually.
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase >pre-integration-test</phase>
<goals>
<goal>wget</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/output</outputDirectory>
<url>http://some.url</url>
<outputFileName>filename</outputFileName>
</configuration>
</plugin>
When I use pre-integration-test it runs during mvn install. However, I just want to run it manually.
Phase none worked for my scenario to keep test-jars from being created for specific modules in my multi-module maven project. Like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
I am trying to set up my POM such that when I do mvn exec:exec or mvn exec:java it will first compile the source and iff successful, execute it.
I have the following and have tried moving the <execution> part about but can't get it to work:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<phase>exec</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>my.main.class</mainClass>
</configuration>
</plugin>
</plugins>
</build>
When I do either mvn exec:exec ... or mvn exec:java it doesn't first compile. I have tried putting the <execution> part in the exec plugin section but that didn't work either?
It's an old topic, but someone else might be interested in an alternative solution for this.
It's not exactly what you were looking for, but you can compile and execute using a single command:
mvn compile exec:exec
This way Maven will always compile the project before executing it.
You can bind the exec plugin to a phase following compile in build lifecycle (verify in the example below):
<profile>
<id>proxy</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<mainClass>my.main.class</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
and than run mvn verify.
I see the answer is very late and you may have found a solution.
I'm just leaving as a reference for others who may need it.
This will not quite answer the question but it will help us to accomplish the main objective you have.
Instead of executing a build goal when running the mvn exec:java we can execute the mvn exec:java when calling a 'post-build" phase/goal.
This will effectively make that phase unusable as it was because we will be substituting it by the execution but if we have a "post-build" goal that we are not using (like package or install) we can use it for the execution like so:
using package goal will end up like:
...
<properties>
...
<main.class>${project.groupId}.MainClass</main.class>
...
</properties>
...
<build>
<plugins>
...
<!-- region Forces the app to be executed when using mvn package -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- endregion -->
...
</plugins>
</build>
This way when we execute goal package with: mvn package the plugin will be executed and the application runned.