How-to integrate a gradle module into a maven project? - java

We have maven parent project with 2 maven modules. Now we need a new maven module, but the building process depends on a gradle build. The problem is, that the new module is a plugin for third-party software and they only provide a gradle plugin for a proper build. Also we need the other modules as dependcies, which isn't a big deal, but we want to have it in the same code repository. My question is now, is it possible to integrate gradle in a maven project. Maybe with the maven plugin, which generates the pom, so that it looks like a maven-module, but we can also use the gradle plugin, if we need so.
We use maven v3 and it's a java project if these informations are relevant.

You can try to use the Maven "exec-maven-plugin".
With this plugin you can run cmd Commands.
So add the plugin and let it execute your Gradle build
<project>
...
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.2.1</version>
<executions>
<execution>
<id>Gradle Executor</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>Your Gradle Executable</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
This should work, give it a try.

Related

How do I use maven shade plugin in Eclipse

I'm a novice here in maven, I'm trying to shade my plugin to add dependencies in my project. But I can't seems to find a way to use maven shade plugin. I would ask that anyone here would show me some examples and explain for me specifically, thanks.
Generally plugins are added to the plugins section of your pom.xml. You need to specify the groupId, artifactId, and version of the plugin you are trying to use. For maven-shade-plugin, you can import it in your pom like this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
This will bind the goals for the Shade plugin to the package phase. Running mvn package will produce a shaded JAR.
Source: https://maven.apache.org/plugins/maven-shade-plugin/usage.html
You can view more examples in the links at the bottom of this page: https://maven.apache.org/plugins/maven-shade-plugin/index.html

Maven: how to query the executable classpath?

I have a maven project with some specified dependencies.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
How can I query maven to find out the path it's using for these dependencies, or the classpath I should use for independent execution?
My goal is to build a wrapper which runs the program with the appropriate classpath.
Several alternatives are available in Maven:
Maven Dependency Plugin (build-classpath goal)
Look at the Maven Dependency Plugin, especially the build-classpath goal provides exactly the full classpath for external execution usages. Among many options, The outputFile parameter may be helpful.
You don't need to configure it for usage, just run
mvn dependency:build-classpath
On your project and you'll see the classpath as part of the build output. Or
mvn dependency:build-classpath -Dmdep.outputFile=classpath.txt
To redirect just the classpath to a file.
Maven Dependency Plugin (copy-dependencies goal)
To build a wrapper, you could also look at the copy-dependencies goal, which would copy the required dependencies (jars), including transitive dependencies, to a configured folder (so you don't need hardcoded paths to your local machine).
An example of plugin configuration is available on the official site, here.
For instance, the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dependencies</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Would add to the folder target/dependencies all the dependencies declared in scope compile. NOTE: with respect to the linked official example, I added the <includeScope>runtime</includeScope> configuration entry (which will include compile and runtime scoped dependencies, according to documentation and my tests), otherwise it would also include the test scope by default (which is something I believe you would not need at runtime).
Exec Maven Plugin (java or exec goals)
Alternatively, you can use the Exec Maven Plugin to execute a main from Maven using the required classpath.
An example of plugin configuration is available on the official site, here.
The following configuration for instance:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>my-execution</id>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.sample.MainApp</mainClass>
</configuration>
</plugin>
Will configure the Exec plugin to run via mvn exec:java the main class MainApp as configured, obviously with the required classpath.
Maven Assembly Plugin
Lastly, the Maven Assembly Plugin also provides facilities to build an executable jar with dependencies, as explained here, in another question on stackoverflow.

Run Maven goal for only one module in multi module project

I have a project which has 4 modules. That said, I have a parent project with a parent POM.xml specifying theses modules.
<modules>
<module>Module1-Desktop</module>
<module>Module2-Core</module>
<module>Module3-Web</module>
<module>Module4-EAR</module>
</modules>
The Module4-EAR has Module3-Web and Module2-Core as dependencies (not as modules) and generates an EAR to be deployed to JBoss.
I want to be able to build the Module4-EAR, but building Module3-Web and Module2-Core before it, so it is up-to-date and also deploy it to JBoss. So I'm running the following Maven command in the parent project:
mvn --projects Module2-Core,Module3-Web,Module4-EAR clean install jboss:hard-undeploy jboss:hard-deploy
In the parent POM I've set the jboss-maven-plugin like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
In the Module4-EAR's pom.xml I've set the jboss-maven-plugin this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<jbossHome>${env.JBOSS_HOME}</jbossHome>
<serverName>default</serverName>
<unpack>true</unpack>
<fileNames>
<fileName>build/Module4-EAR.ear</fileName>
</fileNames>
</configuration>
</plugin>
The problem is that I want only the Module4-EAR.ear to be deployed with to the server, but the Module3-Web.war and Module2-Core are deployed as well. I want to do it in a single command, instead of running 2 mvn commands, one to "clean install" the projects and another to deploy only the EAR. How can I achieve that?
If you only want to deploy the ear then add the jboss-maven-plugin into the pom of Module4-EAR. So the plugin will only work within that module. Adding deployment plugins into a parent pom often leads to executing it into all modules - which is often not what you want.
You can chain the commands so a mvn clean install plugin:goal
The plugin you use seems to be superceded by JBoss AS7 Deployment Plugin: http://docs.jboss.org/jbossas/7/plugins/maven/latest/ (as a side note)
So it would look like mvn clean install jboss-as:deploy
You can also add an execution to the jboss plugin and bind it to the deploy phase so a mvn clean deploy would copy the artifact into a remote repository and deploy the ear into jboss.
An example using the install phase is at: http://docs.jboss.org/jbossas/7/plugins/maven/latest/examples/deployment-example.html
Just had to add this to the only project that I want to execute the jboss:hard-deploy.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
<jbossHome>${env.JBOSS_HOME}</jbossHome>
<serverName>default</serverName>
<unpack>true</unpack>
<fileNames>
<fileName>build/cadprev-web-ear-1.0.0.ear</fileName>
</fileNames>
</configuration>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>hard-deploy</goal>
</goals>
</execution>
</executions>
</plugin>
Removed the code below from the parent POM.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jboss-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
And changed the mvn command to:
mvn --projects Module2-Core,Module3-Web,Module4-EAR clean install

Maven dependencies not being compiled

I have been trying for the last hour or so to get my Maven project to include source files from its dependencies, but for some reason, it isn't. I have followed the steps provided by the following link, but when I compile and run the plugin, I get a ClassNotFoundException:
https://github.com/mkremins/fanciful
I have made sure to include the dependencies and the repository from the link above into my pom.xml file, but when I compile, they don't get added to my .jar file.
I am fairly new to using Maven, and like it so far, albeit that it can be a pain to solve issues like this.
I am building the project by doing the following:
Right click project -> Run As -> Maven Build -> Goal: clean install
EDIT -
With a little more searching around, I figured it wasn't as easy as I thought so. I added the following to my pom.xml build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>mkremins:fanciful</include>
<include>org.json:json</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The only problem with this is that I needed to also manually include the dependencies of the main library I wanted to use - mkremins:fanciful; is there flag or option to automatically copy dependencies from the one file I need, rather than also including <include>org.json:json</include>?
Well, if you want to have your dependencies copied to your target jar, you need to tell maven to do so! Maven doesn't know if the artifact of your project is meant to be self-sufficient executable jar, jar to be executed inside a container or just a dependency or library for another project.
You might want to use copy-dependencies task from maven-dependency-plugin
For example:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}</outputDirectory>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
For more tweaking you might also want to play with jar plugin and assembly plugin. On more about creating executable jars:
http://www.ibm.com/developerworks/java/library/j-5things13/index.html?ca=dat-
You have mistaken the idea of Maven. Maven is intended to use dependencies which are located in Maven Central. It's idea is not to compile dependencies. I recommend you to read about Maven and learn how it works.

Force Maven clean

I have a project with 2 profiles, because UAT and PROD use different versions of the same jar.
I have noticed that if i don't explicitly call mvn clean ... the deployed EAR will have BOTH UAT and PROD jars.
Is there a way in the POM to specify that Maven should always clean before any building?
Use the maven-clean-plugin with the initialize phase as given here
http://maven.apache.org/plugins/maven-clean-plugin/usage.html
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Sure. Visit the Maven clean plugin usage page, they provide an example how to run the plugin automatically during build.
Please read up on the maven lifecycle
especially the package lifecycle.
The maven clean plugin you use will probably allow you to define a clean goal at a particular pahse.
You can also execute for example mvn clean install -P profile

Categories

Resources