Running Gradle from Maven - maven

I'm looking for some Gradle executor plugin for Maven (similar to Maven ant-run plugin).
Google did not help. Is it possible that such plugin doesn't exist?

I should try this:
https://github.com/if6was9/gradle-maven-plugin
This is a maven plugin that makes it easy to invoke gradle from maven.
It is similar to the maven-antrun-plugin that allows ant to be invoked
from maven.

I probably would use:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<executable>./gradlew</executable>
<arguments>
<argument>test</argument>
<argument>-i</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>install</id>
<phase>install</phase>
<configuration>
<executable>./gradlew</executable>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
Because of the following reasons:
I would prefer to trust in a plugin maintained by Codehaus.
If you use the gradle-maven-plugin, probably you'll have to configure additional information in your settings.xml. Otherwise this error can occur: Failed to execute goal org.fortasoft:gradle-maven-plugin:1.0.8

Related

How to resolve the path to an artifact in a maven pom without being a project dependency?

I would like to know if it is possible to resolve the path for a maven artifact without having this artifact as a dependency in the project?
What I am trying to do is to execute an external java code generator via exec-maven-plugin or maven-antrun-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar ${de.powerstat.fb:generator:1.0-SNAPSHOT}</argument>
<argument>de.powerstat.fb.generator.CodeGenerator</argument>
<argument>${fb.hostname}</argument>
<argument>${fb.port}</argument>
<argument>${fb.username}</argument>
<argument>${fb.password}</argument>
<argument>${project.build.directory}</argument>
</arguments>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
So the point here is how to solve the jars path for ${de.powerstat.fb:generator:1.0-SNAPSHOT} without having it as a dependency of the whole project? Also using the exec:java goal is not a solution, because this one seems to have different problems, but thats another question.
Using the antrun plugin I come to the same problem:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<target>
<java fork="true" failonerror="true" module="de.powerstat.fb.generator" modulepath="${de.powerstat.fb:generator:1.0-SNAPSHOT}">
<arg value="${fb.hostname} ${fb.port} ${fb.username} ${fb.password} ${project.build.directory}"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
So question is again how to solve the modulepath="${de.powerstat.fb:generator:1.0-SNAPSHOT}"?
Solutions that use the maven dependency plugin with the goal dependency:properties will not work in this scenario.
I suggest to use dependency:copy to copy the JAR to some place in /target. Then you can add that path to the antrun or exec plugin.

why list the goals of a plugin without binding to a phase?

Please consider this pom excerpt taken from jacoco example ( http://www.eclemma.org/jacoco/trunk/doc/examples/build/pom-it.xml)
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5-SNAPSHOT</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<!-- implmentation is needed only for Maven 2 -->
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- implmentation is needed only for Maven 2 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.60</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Now I know that you can bind a plugin's goal to a maven phase, that is run that goal when maven executes a specific phase.
What is the point of just listing the integration-test goal for the maven failsafe plugin without binding it to something?
The same as for jacoco report and others goal? I don't think you can force the plugin to execute just those listed goals right?
Many thanks
The point is that a plugin can define default life cycle phases where the appropriate goal is bound to (many plugins do this). In this cases you don't need to specify the life cycle phase within the pom file explicitly.
For example the maven-failsafe-plugin has a goal integration-test. This goal has a default binding to the integration-test life cycle phase. Here an excerpt from the documentation:
Description:
Run integration tests using Surefire. Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: test.
The goal is thread-safe and supports parallel builds.
Binds by default to the lifecycle phase: integration-test.
That's the reason why you don't need to give a life cylce phase in the configuration like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
The same is meant for the jacoco maven plugin.
If I understand correctly, M2E will execute the plugin goal during workspace full or incremental builds.
The following article may shed some light:
http://eclipse.org/m2e/documentation/m2e-execution-not-covered.html
You are correct that, on command line, you would not be able to specify the goal, since it is a plugin goal and not tied to a phase. This could be an oddball usage specifically for M2E integration.

How to have exec-maven-plugin:exec run before 'site' phase?

I have this script I need to run before site phase to fix an issue (http://jira.codehaus.org/browse/MSITE-640).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3</version>
<inherited>false</inherited>
<executions>
<execution>
<id>workaround-MSITE-640</id>
<!--it should be 'site' but maven doesn't pick up there-->
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<failWithEmptyArgument>false</failWithEmptyArgument>
<workingDirectory>${project.basedir}</workingDirectory>
<executable>./workaround-MSITE-640.sh</executable>
<arguments>
<argument>${settings.localRepository}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But I can't seem to run it on that phase. I tried to put it under reporting -> plugins but it can't have configuration there.
It is idempotent, so no problem in running it more than once, but would be nice if I could tie it to the correct phase.
Thanks

how to execute multiple command prompt commands using maven in single pom.xml

I want to run multiple command prompt commands in maven using single pom.xml. How can I do that?
For ex: I have 2 commands to execute. I am executing the first command by using exec-maven-plugin.
Below is the portion of my pom.xml to execute the first command:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>load files</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>windchill</executable>
<arguments>
<argument>wt.load.LoadFileSet</argument>
<argument>-file</argument>
<argument>${basedir}/fileSet.xml</argument>
<argument>-UNATTENDED</argument>
<argument>-NOSERVERSTOP</argument>
<argument>-u</argument>
<argument>wcadmin</argument>
<argument>-p</argument>
<argument>wcadmin</argument>
</arguments>
</configuration>
</plugin>
For this the build is success.
Is it possible to execute one more command just like above in the same pom.xml? I was not able to do that. So someone please help how to add it in pom.xml
The answer can be found in the FAQ.
The full answer is here: http://article.gmane.org/gmane.comp.java.maven-plugins.mojo.user/1307
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>id1</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd1</executable>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>cmd2</executable>
</configuration>
</execution>
</executions>
</plugin>
Then you specify execution ids as:
mvn exec:exec#id2
But this syntax is possible starting from maven 3.3.1

how to Include the buildnumber in maven pdf:pdf

I am trying to include the build number in the pdf's that get generated with the maven pdf plugin. I have all the documentation of the project I am working on written as a maven site. This way all the documentation is stored with the source code.
Pom.xml
So in the pom.xml I have defined the buildnumber plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</plugin>
pdf.xml
And in the pdf.xml
<cover>
<coverTitle>${project.name}</coverTitle>
<coverSubTitle>v. ${project.version}</coverSubTitle>
<coverType>Technical documentation</coverType>
<coverVersion>build: ${project.buildNumber}</coverVersion>
<projectName>${project.name}</projectName>
<projectLogo>images/telfortlogo.jpg</projectLogo>
</cover>
I even put resource filtering to ${basedir}/site but it has no effect. I keep getting the ${buildNumber} instead of the result of the buildnumber plugin.
To get something similar working my buildnumber plugin configuration looked like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>pre-site</phase>
<goals>
<goal>create</goal>
</goals>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
<items>
<item>timestamp</item>
<item>${user.name}</item>
</items>
</configuration>
</execution>
</executions>
</plugin>
And in the pdf.xml:
<cover>
<coverTitle>${project.name}</coverTitle>
<coverSubTitle>v. ${project.version} build ${buildNumber}</coverSubTitle>
<coverType>User Guide</coverType>
<projectName>${project.name}</projectName>
</cover>
I believe that your issue comes from the fact that you are executing the buildnumber create goal during the generate-resources phase. And if you are using mvn pdf:pdf or mvn site, the generate-resources will not get executed.
I my setup I have configured the pdf plugin to run on the site phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pdf-plugin</artifactId>
<executions>
<execution>
<id>pdf</id>
<phase>site</phase>
<goals>
<goal>pdf</goal>
</goals>
I can then get the pdf to be generated at the end of the site phase.

Resources