Can I force Maven to build a JAR even though POM packaging specifies WAR? - maven

I'd like to build our project from the command line as a JAR without modifying the POM, which has the <packaging>war</packaging> configuration.
Is there a way to do this?

If you are free to chose how maven is executed it is possible to call maven to invoke the jar goal directly:
mvn jar:jar

I was not able to explicitly build a jar while preventing the building of a war as well. But I realized I didn't have to. With this configuration of the maven-assembly-plugin, and the packaging set to WAR in my POM, a fat-jar and a war will always be created.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<finalName>myApp</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.company.app.Startup</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
If this questions / answer seems confusing / not useful to searchers, feel free to delete it. Thanks to everyone who helped me get here.

Related

maven-assembly-plugin run multiple times, configure to run only once

Small question regarding maven-assembly-plugin please.
I am currently configuring as follow:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>build</id>
<configuration>
<archive>
<manifest>
<mainClass>my.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-mymain</finalName>
<outputDirectory>./</outputDirectory>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This has nothing special, copied from the official website on how to configure.
Now, we use a CI tool, the CI tool, which we do not control, will run the following steps (we cannot change it)
compile:
maven -DskipTests clean package
test
maven verify
stage
maven -skipTests deploy
It will run the three steps separately, again, nothing I can do about it.
Now, the thing it, it seems with the current maven-assembly-plugin as configured above, the uber jar is built once each stage, resulting in a total of 3 jars being built.
How to configure the plugin, to avoid being built and rebuilt, hoping to be built only in one specific phase please?
Thank you
As mentioned by #khmarbaise, this <phase>install</phase> works. All the credit to him, thank you

How to stop maven from output "[WARNING] Configuration option 'appendAssemblyId' is set to false."?

I have a maven project generating a jar and not appending maven-assembly-plugin's appendAssemblyId.
How can I get maven to stop issuing the warning: "Configuration option 'appendAssemblyId' is set to false." ?
EDIT: I was asked to include the pom file, which I'm not permitted to do. I'm including the maven-assembly-plugin block which causes the error.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>some.company.hello</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- prevent appending <descriptorRef> "jar-with-dependencies" to final jar name -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<!-- For inheritance merges -->
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
This answer explains the technical details, but I think the key is the phrase that you mentioned in your question.
I have a maven project generating a jar and not appending maven-assembly-plugin's appendAssemblyId.
If I'm understanding correctly, I take this to mean that in your pom file, you have an entry that says
<packaging>jar</packaging>
What this does is create a jar file that competes with the output of the maven-assembly-plugin, and maven-assembly-plugin wins out, resulting in only one jar file. In fact, if you were to set appendAssemblyId back to true, you will get two files in the output directory - <artifact>.jar and <artifact>-jar-with-dependencies.jar.
So, first, you should remove the <packaging> tag I mentioned above, and add the following to the <executions> section of your pom file so that there won't be two conflicting/competing jar files (and the warning will go away):
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>

Include only certain packages in a Maven generated non-runnable jar

I'm trying to build a non-executable jar file that only includes certain packages from my project to use as a library for common code. I'm planning on deploying that to Nexus and including it in other projects.
I can make it manually in eclipse by right clicking on the project, selecting Export, selecting JAR file (NOT Runnable Jar File) then checking the packages that I want to include. I'm trying to mimic this behavior in Maven. I have been able to replicate the non-runnable part in maven by not including a main in the manifest, but still have to find out how to include only certain packages.
This project actually builds 2 jar files already (from 2 different main's in the program - a test GUI and the regular application). I'm having trouble finding information on how to do the non-runnable jar and selecting certain packages only.
Here is the existing build portion of my pom file is below. Build-third is the non-runnable jar:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-first</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.da.exec.jDaExecutive</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>DaExecutive</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-second</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.gui.jExampleAppGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ExampleAppGui</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<execution>
<id>build-third</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ddslib</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
Tried this myself and it is possible.
Create a new maven profile
Add pluginManagement section to that new profile for maven-jar-plugin and if needed for maven-resources-plugin also
Use this pluginManagement to configure inclusions/exclusions of sources/resources and override artifacts final name here to have different name for that separate jar
Example
<profiles>
<profile>
<id>subset</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>subset</finalName>
<includes>
<include>org/example/stuff/**</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Using this profile maven creates subset.jar that has everything under org/example/stuff/**
See more about excluding: How to exclude a set of packages from maven build jar?
Of course it might be possible to do all this also without pluginManagement but in my opinion pluginManagement provides clean & flexible way to configure this kind of stuff so i recommend that.
You should be able to do that by using a custom assembly descriptor and specifying your fileset and / or file includes / excludes. See https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet

how to add my external jar file to class path

I am new to maven environment, need some ones help.
Added my external jar file (directoryhelper.jar) in lib folder as below in pom.xml
<dependency>
<groupId>com.test.directoryhelper</groupId>
<artifactId>DirectoryHelper</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/directoryhelper.jar</systemPath>
</dependency>
compilation is successful, but during run time I am getting
java.lang.NoClassDefFoundError.
how to add the directoryhelper.jar to class path.
Maven out of the box will come up with a JAR file (default packaging). This JAR file only contains (main) artifacts of the project. If you take just that and run it, clearly the dependencies are missing -- by design.
Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. Edit: if you're using APKs and installing them on a phone, there may be mechanisms to deal with dependencies, I'm answering this merely from a Maven standpoint.
If you want to create a JAR with dependencies you have to tell Maven to do so, that's not the default.
Ways of having Maven do that are (probably not exhaustive):
Maven Assembly plugin, jar-with-dependencies predefined descriptor:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
...
Maven Shade plugin
That way it'll create a single-jar of large size and build time will be large everytime you try to build.
I instead prefer adding all jars to a lib folder and including in the classpath (jar's manifest), because of which when we have to make some change or redeploy to the client or some place, we can simply give the small jar (not all the dependencies merged within jar)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.kalindiinfotech.webcrawler.MainGUI</mainClass>
<!-- <mainClass>com.KalindiInfotech.busbookingmaven.form.LoginForm</mainClass>-->
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Recommended practices for distributing/using maven projects outside the build lifecycle

There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.
Things I have tried
1) Maven-shade plugin
For some use cases ( such as hadoop jobs ) I find it convenient to use the Maven shade plugin to integrate with the package step ( it builds an all inclusive uber-jar ). The downside is that the uber-jar is too massive. Also, I cant get the maven shade to work on datanucleus dependencies as it messes up something.
2) distribute the dependencies along with the jar.
//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass
generates a directory with all the dependencies along with the jar.
What I would like to do is
3) just be able to distribute the jar and handle the dependencies while executing the jar. Since mvn package puts the pom in the jars manifest folder, all the information is there right ? . Now, I would like it if there was a one line command to be able to run this jar asking maven to manage the dependencies. Even more awesome if someone knows if such a thing can be used as a hadoop job.
I think there is no solution for question in your point 3.
My three options, in all cases you can run application with simple java -jar target/project.jar. Nothing more is needed.
1. Maven Assembly Plugin with jar-with-dependencies descriptor
(manifest in jar configuration is missed here)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
2. Maven dependency plugin
Maven dependency plugin, properly jar configured (classpath) and maven assembly custom descriptor for packing all together.
(you can simplify this, if your project is not using snapshot dependencies)
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.package.MainApp</mainClass>
<packageName>your.package</packageName>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}.$${artifact.extension}/customClasspathLayout>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeScope>test</excludeScope>
<includeScope>compile</includeScope>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
3. Maven One Jar
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Resources