Maven maven-exec-plugin multiple execution configurations - maven-2

Is it possible to invoke a maven-exec-plugin (or any other plugin's) execution by its id from the command line?
Let's say my pom.xml file looks like this:
<project>
[...]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>foo</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>bar</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>bar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
[...]
</project>
Now is it possible to call
mvn exec:exec
with some added magic to run execution "foo"?
For the curious there is an alternative solution using profiles available here:
http://www.mail-archive.com/user#mojo.codehaus.org/msg00151.html

It is now possible, starting from Maven 3.3.1: see improvement MNG-5768 and Maven 3.3.1 release notes
You can call a specific execution configuration with this syntax:
mvn exec:exec#foo

No, it's not possible. Executions are for binding to the lifecycle (i.e. they are not designed to be invoked on the command line). So you'll have to use the profile trick described in the link that you provided.

Not mentioned here is that, as of Maven 2.2.0, if you give an execution of any plugin the id "default-cli", then when you run that plugin from the command line, that configuration is used. You're limited to only one default execution of each plugin, but it's a start.

I think if you write execute the goal:
org.codehaus.mojo:exec-maven-plugin:¿Version?:exec
it worked for me in eclipse maven plugin.

Related

Chaining Maven Flyway commands

How can Flyway commands be chained into a single command using Maven?
For example, I want to run mvn initialize flyway:clean followed by mvn initialize compile flyway:migrate. However, mvn initialize flyway:clean compile flyway:migrate fails.
Thanks!
I just checked
mvn initialize flyway:clean compile flyway:migrate
with both Maven 2.2.1 and Maven 3.0.3 and it works every time.
Could you double check this? In case you think you really found a problem, please file an issue in the Issue Tracker with the necessary steps to reproduce it and I'll do my best to fix it asap.
That will give you the ability to chain maven steps, just add the goals you want in there
<profile>
<id>clean-migrate</id>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>process-resources1</id>
<goals>
<goal>resources</goal>
</goals>
<!-- Populate the database before querydsl-sql runs -->
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<executions>
<execution>
<id>process-resources2</id>
<goals>
<goal>clean</goal>
<goal>migrate</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<version>1.4.2</version>
<configuration>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:#${database-hostname}:${database-port}:${database-sid}</url>
<user>${database-username}</user>
<password>${database-password}</password>
<schemas>${database-schema}</schemas>
<table>schema_history</table>
<initialVersion>0.1.00</initialVersion>
<initialDescription>Base Migration</initialDescription>
</configuration>
</plugin>
</plugins>
</build>
</profile>
You will also need the following in your settings.xml
<profile>
<id>inject-flyway-properties</id>
<properties>
<database-hostname>${env.DB_HOSTNAME}</database-hostname>
<database-port>${env.DB_PORT}</database-port>
<database-username>${env.DB_USER}</database-username>
<database-password>${env.DB_PASSWORD}</database-password>
<database-sid>${env.DB_DEFAULT_SID}</database-sid>
<database-schema>${env.DB_SCHEMA}</database-schema>
</properties>
</profile>
If i understand the docs correct you have to configure the maven-flyway plugin to do the work and bound it to the correct phases of maven than you can use the default maven calls like mvn clean package or mvn clean verify.
You can use profiles in order to bundle "commands" together. Once it's done you just call:
mvn -Pmy-profile

Maven annotation processing with maven-compiler-plugin

I try to compile my code that contains annotations that generate source code. I use the maven-compiler-plugin and build-helper-maven-plugin. My POM is looking like that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/apt</generatedSourcesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/apt</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn compile, ${project.build.directory}/generated-sources/apt is added as a source directory, and the generated sources are generated in the correct directory. But I get compiler errors because of missing references to the generated classes. It's like the generated source directory is not included in the compilation process.
I also try apt-maven-plugin which does not generate anything. And maven-annotation-plugin but it behaves as describe above.
The release 2.0.7-SNAPSHOT of maven-annotation-plugin should solve the problem
fwiw I just downgraded from 3.0 to 2.5.1 and fixed an issue I was seeing with APT processing not being executed after a few runs, using maven so just downgraded one version:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.maven.plugins%22%20AND%20a%3A%22maven-compiler-plugin%22
Can you try with the latest version of maven compiler plugin (2.3.2)?
Also build-helper-maven-plugin is not required since it looks like you are using the maven-compiler-plugin itself to generate the sources from the annotation.

Get name of maven pom file

I am running a Maven (2) release build with with: mvn -f release.xml clean deploy
and want to get the currently running pom file name (release.xml) into a property or mojo.
Is it possible?
well you can use the property
${env.MAVEN_CMD_LINE_ARGS}
in your pom or a filtered resource, which will expand to something like
clean install -Paxis -Dmaven.test.skip -f mypom.xml -pl util
however, if you just want the mypom.xml part, you're going to have to do some scripting, which is not supported out of the box in maven. Common solutions are either maven antrun plugin or gmaven (groovy) plugin. Here's a way to do it in gmaven:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.out.println(
System
.getenv("MAVEN_CMD_LINE_ARGS")
.replaceFirst( /.*\-f\s+(\S+).*/ , 'POM File: $1')
);
</source>
</configuration>
</execution>
</executions>
</plugin>
Edit: as you want it in a property, either use System.setProperty or write directly to project.properties

Is it possible to single out and run a specific goal bound to a maven phase?

Updated to (hopefully) clarify: If a goal is defined to run during a given phase, is it possible to run the individual goal without running thru all phases. In other words would it be possible to run the antrun:run goal (which is defined as part of the install phase below) without getting dependencies, generate-resources, compiling, testing, package, etc?
I'm using the antrun plugin to create a zip file during the package phase and to delete and copy some files during the install phase. I understand how to run single maven plugin goals, for example: mvn antrun:run. However, is there a way to run a specific execution's goal? Something like mvn antrun:run:execution-id, or mvn phase:antrun:run?
Basically, I'd be nice if I can tell maven to do nothing else but run the ant tasks defined below inside the deploy phase, for example. It's kind of tedious having to wait for maven to go thru all the phases just to check if the ant tasks in the deploy phase are working correctly.
<executions>
<!-- create zip file -->
<execution>
<id>create-zip</id>
<phase>package</phase>
<configuration>
<tasks>
...create zip...
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<!-- do some other stuff -->
<execution>
<id>copy-files</id>
<phase>install</phase>
<configuration>
<tasks>
...delete some files, copy some files ...
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
In other words would it be possible to run the antrun:run goal (which is defined as part of the install phase below) without getting dependencies, generate-resources, compiling, testing, package, etc?
No it's not. While you can configure a plugin (with a <configuration> section under the <plugin> element) and call in on the command line, you can't invoke a specific executionid (and consequently the <configuration> specific to an <execution>).
The only solution in your case would be to declare the antrun plugin in a profile, let's say my-profile, to duplicate the following part of the configuration to configure the plugin in this profile:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<configuration>
<tasks>
... delete some files, copy some files ...
</tasks>
</configuration>
</plugin>
and to call with the right active profile:
mvn antrun:run -Pmy-profile
Try the exec maven plugin...
For ex:
When you run jboss with maven, you can't see the jboss console output, but I need it to display, so what I did is I wrote a java file that reads in server.log(the server console output) as it changes to display the changes so it appears that jboss console is actually showing(a bit hack-ish but working). So I come to the point of answering your question, during the pre-integration-test I executed a java goal which starts my java program. Here is how , using execute plugin of course :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>console-start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.eclipse.console.Main</mainClass>
</configuration>
</plugin>
You just run the install and it executes during the pre-integration-test, however if you just want to execute something like java, use the execute plugin. Sorry if the answer not appropriate, I didn't have the patience to read your question in details, my work hours are over .. cheers

How to run individual test in the integration-test target in maven

We have a hundreds of tests defined for our integration-test phase lifecycle in maven, and they take a long time to finish.
What I want to do is run just one test in the integration-test. I tried doing :
mvn -Dtest=<my-test> integration-test
but that does not work. The -Dtest runs only the tests in the unit test goal, not the integration-test phase. I tried the -Dintegration-test=<my-test> instead, and that was ignored.
Is there a way to do that ?
My configuration is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/api/**</include>
</includes>
.....
If you're using the Maven failsafe plugin, you can run a single integration test by setting the it.test property to your fully qualified test class name:
mvn -D it.test=your.TestCase verify
See the failsafe plugin docs for more info.
The Failsafe documentation would have you specify the test like so:
mvn -Dit.test=BrokenIT verify
However, -Dit.test does not appear to work any longer. Rather, the same parameter used to specify a Surefire test will apparently work for Failsafe as well. For example:
mvn -Dtest=WorksIT verify
I've filed a bug (EDIT: which was closed as "Cannot Reproduce" in 2.12) to correct the documentation.
just add -DfailIfNoTests=false works for me with testNG. Something like this:
mvn integration-test -Dtest=aITest -DfailIfNoTests=false
I struggled through this, and I created an additional profile to use when I wanted to run just one integration test. I hope that I've successfully extracted just the right part here:
<profile>
<id>integrationTestSingle</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>surefire-it</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/integration/**/${test}.java</include>
</includes>
<skipTests>false</skipTests>
</configuration>
</execution>
</executions>
<configuration>
<argLine>-Xms256M -Xmx768M -XX:MaxPermSize=256M</argLine>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<configuration>
<skipTests>true</skipTests>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Now, I call maven with the integrationTestSingle profile and with -DfailIfNoTests=false -Dtest=NameOfTest, and it doesn't run any of the regular tests during the regular "test" phase, and runs just the NameOfTest test during the integration-test phase.
I'm not sure about JUnit, but for TestNG the strategy would be to define a suite XML file with only the one test, and then in your POM configure the surefire plugin to only run that. In your POM, you would have something like this (disclaimer, this is untested):
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>single-test.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
To configure the suite file, see http://testng.org/doc/documentation-main.html
Just ran into this myself. Something like this worked well for me:
mvn -Pintegration-test -Dtest=<my-test>
The trick was to ensure that the test-group was mentioned before the -Dtest portion.
I am using:
Apache Maven 3.6.3
openjdk version "11.0.2" 2019-01-15
<groupId>org.codehaus.mojo</groupId>
<artifactId>failsafe-maven-plugin</artifactId>
<version>2.4.3-alpha-1</version>
This command worked for me:
mvn failsafe:integration-test -Dsurefire.skip=true -DskipIntegrationTests=false -DskipTests=false -Dtest=com.myxyz.func.ITestGate
Was actually simpler that the answers above by going back to basics with the actual documentation.
Running Junit 5 integration tests:
openjdk version "11.0.11" 2021-04-20
Apache Maven 3.6.3
In the main build just drop in documented failsafe config:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then you can run only a specific integration test with:
mvn -Dtest=\*cs1_\* verify
Note that this version will run your tests in the target folder and if you need to load any external files that are something like src\test\resources\x.y then they are copied to target\test-classes\x.y
This works for me, when I gonna run only one test method in integration test
mvn clean -Dit.test=some.package.SomeTestClass#testMethodName integration-test