I'm using maven-jar-plugin in pom.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<!-- DO NOT include log4j.properties file in your Jar -->
<archive>
<manifest>
<!-- Jar file entry point -->
<addClasspath>true</addClasspath>
<mainClass>com.example.Manager</mainClass>
<classpathPrefix>dependency-jar/</classpathPrefix>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
I can see in logs that maven execute it:
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: example.jar
[INFO] [jar:jar {execution: jar}]
but it don't put main class to MANIFEST.MF:
Manifest-Version: 1.0
Built-By: romkazanova
Build-Jdk: 1.7.0_65
Created-By: Apache Maven
Archiver-Version: Plexus Archiver
i don't understand why. I work with Idea.
You have to define the configuration in a pluginManagement area cause the maven-jar-plugin is already bound to the build life-cycle
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.Manager</mainClass>
<classpathPrefix>dependency-jar/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
The problem you have is that the execution you have defined means to have a supplemental execution as the log output shows and not change the configuration of the existing execution within the life cycle.
Furthermore you seemed to be running on Maven 2 instead of Maven 3 and you should update as soon as possible because Maven 2 has been defined EoL
You can of course add the version of maven-jar-plugin.
Try to compile manually by prompt: mvn compile
or try: mvn clean install -Dmaven.test.skip=true
What IDE are you using?
#khmarbaise is wrong in his definition of PluginManagement. According to the definition in Maven documentation, PluginManagement is intended to configure project builds that inherit from this one. In your pom.xml you have no needs to keep the PluginManagement node.
You only have to change the location of your configuration node from execution level to plugin level. You can see that #khmarbaise did that too.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<!-- DO NOT include log4j.properties file in your Jar -->
<archive>
<manifest>
<!-- Jar file entry point -->
<addClasspath>true</addClasspath>
<mainClass>com.example.Manager</mainClass>
<classpathPrefix>dependency-jar/</classpathPrefix>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Furthermore you don't have to keep the executions node anymore because maven-jar-plugin is already bound to the build life-cycle as #khmarbaise mentioned it.
Related
Currently I have a multi module project with maven, in the super pom I have added the modules of the projects. From the SparkAppBuilder module I intend to build the jar project that contains the SparkDriver and SparkProcess projects.
To do this from the SparkAPPBuilder module, add the maven-assembly-plugin plugin, I see that it builds the project by adding my 2 modules, but the problem is that it is generating the jar with all the dependencies that are in my local repository, so the jar weighs more than 150MB.
This is my SparkAppBuilderPOM:
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.streaming.example.DirectStreaming</mainClass>
</manifest>
</archive>
<finalName>JavaStreamingDirect</finalName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.streaming.example.DirectStreaming</mainClass>
</manifest>
</archive>
<finalName>JavaStreamingDirect</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
...
<dependencies>
<dependency>
<groupId>com.spark.driver</groupId>
<artifactId>SparkDriver</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.spark.streaming</groupId>
<artifactId>SparkProcess</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
Do you know why you add my entire local repository in the assmbly phase?
Thanks so much.
Regards
A jar-with-dependencies also contains all transitive dependencies. Those are usually needed to run the project because they are the dependencies of your dependencies.
So it is not unusual that this jar is large. You should look in your mvn dependency:tree where most of the stuff comes from -- then you might be able to reduce the amount by changing the dependencies of your modules.
So: The jar-with-dependencies does not contain the whole local repository, just the whole dependency tree.
My pom.xml looks like this
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
...
but although I specified the main class, on clean package install maven only creates jars that aren't runnable.
How can I fix that? The manifest then looks like this:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Matthias
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_121
Instead of maven compiler plugin, you should configure the maven jar plugin to create an executable jar :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To declare the source/target java version you could use :
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
instead of declaring the compiler plugin.
This is less verbose and it produces exactly the same result.
Besides, if you want to include dependency jars of your application inside your executable jar, you should favor the maven assembly plugin over the maven jar plugin :
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
<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>
I'm new to maven and currently try to assemble a scala project with it. Project structure:
dir
|
|--src/main/java
|
|--src/main/scala
|
|--pom.xml
I was kind of surprised that classes compiled from *.java end up in jar, but one compiled from *.scala do not. I added these plugins to pom.xml
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
I though maven-jar-plugin is responsible for assembling jar files. But for some reason, it does not add scala-compiled classes.
QUESTION: Who adds .class file into a final jar after executing mvn install? How to add .class-files compiled with scala compiler?
mvn package will build your jar, however maven-compiler-plugin will only compile your java source files not your scala source files. Scala-maven-plugin can be used to compile both java and scala sources.
I wrote a blog post on this a while ago, that may help http://blog.rizvn.com/2016/04/scala-and-maven.html
You will need to tell maven about src/main/scala, since you are putting your scala code under src/main/java. This is done through the build section like so:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
...
</build>
Can you try something like this, using "scala-maven-plugin" instead. Then execute maven goal : mvn clean package
<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<configuration>
<sourceDir>${basedir}/src</sourceDir>
<outputDir>${basedir}/target/classes</outputDir>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
I'm using the minijar-maven-plugin to reduce the size of my jar-with-dependencies jar but I need to specify a mainClass like I can do easily with the maven assembly plugin. How can i specify the mainClass in the manifest while still using the minijar plugin?
My minijar configuration is the default:
<plugin>
<artifactId>minijar-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>ueberjar</goal>
</goals>
<configuration>
<includeArtifact>true</includeArtifact>
<stripUnusedClasses>false</stripUnusedClasses>
<includeDependencies>
<param>org.vafer:dependency</param>
</includeDependencies>
<includeDependenciesInRelocation>
<param>org.vafer:dependency</param>
</includeDependenciesInRelocation>
</configuration>
</execution>
</executions>
</plugin>
I can specify the main class in a maven assembly plugin using:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.chheng.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
You should try the maven shade plugin. It deals better with metadata and also take care of the dependency inlining.
I've deprecated the minijar plugin and instead added support for the same optimizatiosn to the maven shade plugin.
Watch/vote for this issue to get it applied.
This has been requested - see this executable uberjar thread and MOJO-852 - but is still not supported. And given that this issue is open for more than 3 years now, I would not expect a fast resolution (unless you submit a patch).
I don't know the minijar plugin very well, I never used it actually... but what happens if you configure the jar plugin to generate a manifest with the main-class entry for the main jar?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!--addClasspath>true</addClasspath-->
<mainClass>my.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I want to find a maven native (i.e. without calling external programs) to inject the svn revision in the war manifest.
Does anybody know a way to do that?
I found mention to how to add the subversion revision to manifests in jar files but not with war files.
I searched SO but could not find this issue specifically.
I want to find a maven native (i.e. without calling external programs) to inject the svn revision in the war manifest.
This is possible with the Build Number Maven Plugin using the svnjava provider:
If you need to execute the plugin on
machine without any svn in the path
you can configure the mojo to use the
svnjava provider.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
</plugin>
</plugins>
</build>
The Build Number Maven Plugin sets the build number in the ${buildNumber} property that you can then use in your POM.
I found mention to how to add the subversion revision to manifests in jar files but not with war files.
Then, to add the build number in the MANIFEST of a war, configure the plugin as mentioned in the Usage page:
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Try this. About halfway down, look for maven-war-plugin
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>