is maven-assembly-plugin effective in this case - java

I have following project structure:
Project "parent-project" does not have any source file and has subprojects as "junit-wrapper","child1-test" and "child2-test".
Subproject "junit-wrapper" has only java source inside src/main/java and this is basically created to wrap all the dependencies and binaries under the hierarchy "parent-project".
Subproject "child1-test" and "child2-test" has no source files and only contains subprojects as "child1-env" and "child2-env".
Subproject "child1-env" and "child2-env" has only junits inside src/test/java.
I want to build a super jar(within junit-wrapper) by building parent pom.xml
I hope this is possible by using maven-assembly-plugin but don't know how to configure this in pom.xml. what should be my pom.xml or assembly.xml(on using plugin) entries in order to ensure this is achieved?
please suggest.
thanks.

To create a jar which contains test-classes the best solution is to use the maven-jar-plugin like this:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
In other modules you can use the test-jar via the following dependency:
<project>
...
<dependencies>
<dependency>
<groupId>com.myco.app</groupId>
<artifactId>foo</artifactId>
<version>1.0-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>

You will get your "uber-jar" when you include this configuration into the pom file of junit-wrapper:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
An assembly descriptor (assembly.xml) is not necessary because the jar-with-dependencies descriptor is already available within the maven-assembly-plugin. Note that you should not execute the assembly plugin before the package phase. Otherwise the code of your maven module will not get packaged into your assembly.

Related

Maven: How to create a jar file of source files and test files?

I have a Maven project that also includes unit tests. I want to create a JAR file from both code files placed under 'src' folder and code files placed under 'test' folders. Currently, I use maven-jar-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
<execution>
<id>make-a-test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
This plugin generates two jar files - one file for source and another one for tests. I want to create one Jar file for both.
You can create a separate jar file only from the src/test area in your project
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
That will create a separate jar file which can be used by other like this:
<project>
...
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<classifier>tests</classifier>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
But usually if you like to have a separate test-jar it makes more sense to create a separate project/module which contains the source code and you only change the scope of the dependency. This is described in the docs as the preferred way. which I strongly recommend to go.

Maven : Deploy Local Project to Remote Server with dependencies

I know this can sound very noobish and I apologize for that.
My question is simple : I developed a project locally, it contains many external dependencies that are referred to in the pom.xml file.
When I deploy on the remote server using mvn deploy, only the application jar is deployed, but not its dependencies. So I end-up with java.lang.NoClassDefFoundError when I try to execute my program on the remote server.
What do I need to do to make this work in a proper way ?
EDIT : I would rather avoid ending up with a massive sumo jar with all dependences in it. I would prefer to export the dependencies separately to the remote server (if that makes sense)
EDIT 2: Is there a way to "Mavenize" the remote server and execute Maven Dependency lookup directly from there ? And only deploy my "real" jar when I update the code ?
I will have a look at the maven-dependency-plugin with the dependency:copy-dependencies mojo. Looks interesting.
Below my pom.xml : jar-with-dependencies did not work (I must have missed something)
<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</groupId>
<artifactId>zylon</artifactId>
<version>HBaseConnect</version>
<name>BaseConnect</name>
<dependencies>
(...)
</dependencies>
<distributionManagement>
<repository>
<id>ssh-repository</id>
<url>scpexe://remote.server/cloud/repo</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>zylon.myMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh-external</artifactId>
<version>1.0-beta-6</version>
</extension>
</extensions>
</build>
</project>
The shade plugin below worked though, but it results in a massive Jar file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
You can achieve this using the maven-assembly-plugin and configuring it to use the jar-with-dependencies descriptor.
You can find examples and more details of this here
EDIT: Make sure to define an execution goal for your plugin and invoke such goal correctly; either by:
Specifying the fully-qualified goal mvn groupId:artifactId:version:goal (you can check how to shorten this reference here)
OR
Attaching the goal to the desired maven phase (such as package). In your particular case:
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
As showcased in the original link I attached (https://maven.apache.org/plugins/maven-assembly-plugin/usage.html)

How to make sure that "java" finds the all the required classes when IDE can?

I have a Maven project with only one dependency :
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
I am using an IDE and it installed all the required dependencies itself. I have written a simple code that uses the classes from net.sf.jasperreports. To create a the package I did mvn package and it created the jar. When I try to run the jar as java -jar myjar.jar, I get an error saying Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRDataSource
I cannot understand how to resolve this. The IDE is able to find the classes but when invoked from command-line, it fails to find the classes. Why is that? What should I do?
I even tried to keep the jasperreports-6.8.0.jar at /Library/Java/Extensions/ but that too did not work.
Here is how the pom.xml looks like:
<?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.suhail</groupId>
<artifactId>JasperCSVDataSource</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
</dependencies>
</project>
This is the part that uses the maven shade plugin in my project, which is of course totally different from yours:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<artifactSet>
<includes>
<include>org.apache.poi:poi</include>
<include>org.apache.poi:poi-ooxml</include>
<include>org.apache.poi:poi-ooxml-schemas</include>
<include>org.apache.commons:commons-collections4</include>
<include>org.apache.xmlbeans:xmlbeans</include>
<include>org.apache.commons:commons-compress</include>
</includes>
<excludes>
<!-- Exclude main jar to avoid error 'Failed to create shaded artifact,
project main artifact does not exist' -->
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
Please note that this part of the pom.xml goes into the <plugins> ... </plugins> section, in my case it is below the maven compiler plugin and below the maven jar plugin. You can explicitly include certain dependencies or explicitly exclude certain ones.
The result will be 2 files in your target folder, I personally use the one that has the original name, the other one will have the original name extended by shaded or similar.
If you get an error stating No main manifest attribute in ... then change your configuration of the maven jar plugin as follows:
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</configuration>
Use Maven assembly plugin instead Maven jar plugin.
see what-are-the-differences-between-maven-jar-plugin-and-maven-assembly-plugin!
Maven assembly plugin creates a fully deployable package with all dependencies packed in it.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Probleme to make a JAR using External JAR with MAVEN [duplicate]

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 6 years ago.
With a friend we work on a project for our school and we are at the end.
The Probleme is that with Eclipse, our java program work correctly, and with the JAR generated by eclipse too. But we need to, when you exec the command mvn package, to generate a JAR. This JAR don't find classes of External JAR, it's the first time we use Maven so can you please help me to make a good pom.xml that when we do mvn package it generat a JAR using External JAR ?
There is the pom :
<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>coo.project</groupId>
<artifactId>COO-DONJON</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>COO-DONJON</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>javazoom</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>coo.project.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
You will have to use the Maven Assembly Plugin.
What you need is an executable jar which should contain all your dependencies. Check below for a sample code.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>coo.project.App</mainClass> // your main class here.
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This will produce two jars. One of them will be a regular jar, and the second one will be a fat jar with -jar-with-dependencies appended to its name. This is the jar that you should be using now. This fat jar will include your code as well as all the dependencies that your code needs.
If you do this, I don't think you will have to configure the maven-dependency plugin as well as the maven jar-plugin as done in your pom.xml. You can ignore that.
You need to use Maven shade plugin to add all dependencies inside jar by creating uber/fat jar.
Check this http://howtodoinjava.com/maven/maven-shade-plugin-create-uberfat-jar-example/

Is there any way to change mywebapp-1.0-SNAPSHOT-classes.jar from attachClasses configuration in maven-war-plugin to mywebapp-1.0-SNAPSHOT.jar?

According to maven-war-plugin FAQ,
If you can't move the classes to another project, you can deploy the classes and resources included in your webapp as an "attached" artifact, with a classifier, by using the following configuration:
<project>
...
<artifactId>mywebapp</artifactId>
<version>1.0-SNAPSHOT</version>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.
Is there any way possible that I could get mywebapp-1.0-SNAPSHOT.jar instead of mywebapp-1.0-SNAPSHOT-classes.jar?
Update: :
I want both war as well as jar to be generated. Though I am able to do this by applying profiles as mentioned in Changing packaging based on active profile in pom. But I am curious to know about above question.
Short answer: no, you can't.
By looking at the maven-war-plugin sources, in WarMojo.java (line 292), you can see that you cannot meet your requirement by simply modifying the maven-war-plugin configuration:
protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
{
if ( classifier == null )
{
classifier = "";
}
else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
{
classifier = "-" + classifier;
}
return new File( basedir, finalName + classifier + "." + type );
}
Since the classesClassifier parameter is "classes" by default, you have no chance to modify this behavior: classifier will never be null.
Either you rename the file after its generation, or you modify the source code of the maven-war-plugin, and use it as a custom plugin.
As a final consideration, it is not advisable to have a null classesClassifier: it could be misleading. I do not know your project requirement though.
Hope it helps.
The closest approach to what you want to do is to use <classesClassifier>
as
<project>
...
<artifactId>mywebapp</artifactId>
<version>1.0-SNAPSHOT</version>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>someClassifier</classesClassifier>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
but this approach will always put the classifier as your_jar-classifier.jar and if you create and empty or spaced tag, it will default to -classes
On the other hand, by using archiveClasses instead of attachClasses you will find the JAR just as you want it inside war's WEB-INF\lib (without the classifier), but no WEB-INF\classes will be generated.
Try to configure your webapp's pom.xml using the following <build> section, to see if it fits your needs :
...
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>create-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>attach-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
This section asks Maven to explicitly create a jar, then it attaches the created jar to the build so that the install phase can copy the generated artifact into the local repository.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.build.finalName}/WEB-INF/lib/${project.build.finalName}.jar</file>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Combining some of the ideas mentioned above seems to work for me. The <archiveClasses> (as explained here) causes the required JAR to be generated within the WAR file and the attach-artifact goal extracts that file as an artifact.
Maven's general philosophy is that one POM generates one artifact. That would mean the cleanest, most "Maveny" way to get the Jar out of your project is to separate your Java code and the Webapp items into two modules grouped together by a parent project.
So a potential project hierarchy could be:
mywebapp-parent/ -> Contains all dependency info, project version
mywebapp/ -> Creates Jar
mywebapp-web/ -> Depends on mywebapp, Creates War

Categories

Resources