"no main manifest attribute" while running Jar after building with maven - maven

I have a problem getting a maven build to work properly. It builds just fine, except the Main-Class within the manifest file found in the JAR is not present.
Currently, I have one parrent POM, and one POM for each of two modules. The reactor summary shows a success on all three buils.
However, when I try to run the jar in one of the modules, I am told "no main manifest attribute" exists. Checking the content of the manifest-file within the jar shows the Main-Class is not set:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: user
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_66
This is the build content of the parrent POM:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<executable>${java.compiler}</executable>
<verbose>${java.compiler.verbose}</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<quiet>${javadoc.quiet}</quiet>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<module>ConsolidatedCustomerMasterEndpoint</module>
<module>ConsolidatedCustomerMasterService</module>
</modules>
And this is the build content of the module POM:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>no.xyz.ccm.config.CcmApp</mainClass>
<classifier>jetty</classifier>
<requiresUnpack>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>CcmService</artifactId>
</dependency>
</requiresUnpack>
<addResources>false</addResources>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven-build-helper-plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/generated/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<delimiters>
<delimiter>#</delimiter>
</delimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

Related

Exclude log4j.properties in the generated jar

I am using maven-compiler-plugin to build a JAR from one of my module, and I am not able to exclude log4j.properties from the generated JAR.
I tried:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
But this does not work. I can exclude the file globally:
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>log4j.properties</exclude>
</excludes>
</resource>
</resources>
But I don't want to do this as this impact the Eclipse build, and makes it impossible to run code from Eclipse that uses log4j.
I tried to see if the included log4j.properties was mine and not from another module, and it is mine. I've read numerous posts on the issue, including some here, but I still cannot solve it...
If this matters, this module is a submodule of a parent package, with the following <build>:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
<configuration>
<show>private</show>
<nohelp>true</nohelp>
</configuration>
<executions>
<execution>
<id>create-javadoc</id>
<phase>javadoc</phase>
<goals>
<goal>javadoc</goal>
</goals>
</execution>
</executions>
</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>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>

combination of shade, proguard and appassembler maven plugins

I'm trying to build and obfuscate a multi module project using maven. I use the shade plugin to create a fat jar containing all of my own class files(every module) so that I could obfuscate the fat jar using proguard-maven-plugin and then create executable build output using appassembler plugin. everything works except that the other module dependencies also appear in the appassembler repo dir, which is wrong because the obfuscated classes already exist in the shaded jar.
I've tried defining the other module dependencies as provided and then adding the dependencies for the shade plugin, but the shade plugin seems to ignore them.
this is the relevant part of pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-a</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-b</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<injar>${project.build.finalName}-shaded.${project.packaging}</injar>
<outjar>${project.build.finalName}.${project.packaging}</outjar>
<proguardInclude>proguard.pro</proguardInclude>
<maxMemory>1024m</maxMemory>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<programs>
<program>
<mainClass>my.package.Application</mainClass>
</program>
</programs>
<useWildcardClassPath>true</useWildcardClassPath>
<repositoryLayout>flat</repositoryLayout>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any ideas are welcome.
I found a solution which is not as convenient as I'd like but its better than removing the other module jars manually. I used assembly plugin to exclude the jars from the build distribution zip.
pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.build.finalName}-shaded.${project.packaging}</outputFile>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.13</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<injar>${project.build.finalName}-shaded.${project.packaging}</injar>
<outjar>${project.build.finalName}.${project.packaging}</outjar>
<proguardInclude>proguard.pro</proguardInclude>
<maxMemory>1024m</maxMemory>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>2.0.0</version>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
<configuration>
<programs>
<program>
<mainClass>my.package.Application</mainClass>
</program>
</programs>
<useWildcardClassPath>true</useWildcardClassPath>
<repositoryLayout>flat</repositoryLayout>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>descriptor.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
descriptor.xml:
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/appassembler</directory>
<excludes>
<exclude>**/module-a-${project.version}.${project.packaging}</exclude>
<exclude>**/module-b-${project.version}.${project.packaging}</exclude>
</excludes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
I think your issue comes from the fact that the shaded jar and the appassembler are ran during the same phase, package.
I think you should try to modify the phase of the appassembler plugin to:
<phase>post-package</phase>

Build maven jar from classes no java source

I want to build a maven jar artifact from classes. I don't have source files. These classes are originally in another artifact installed locally. I use maven-dependency-plugin to unpack the classes and put them in the target folder for this project/module.
It creates the jar.. but doesn't include the classes I just unpacked. Here's my pom:
<build>
...
<!-- unpack myjar1.jar and myjar2.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.company</groupId>
<artifactId>myjar1</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target/final</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.company</groupId>
<artifactId>myjar2</artifactId>
<version>1.0</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>target/final</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>/path/to/target/final/folder</classesDirectory>
<includes>
<include>**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
How can I include these classes into my final.jar?
I think the best solution is the maven-shade-plugin: create a pom.xml, add those 2 libraries as dependencies and configure the maven-shade-plugin.
Run mvn package and you have your merged project.
What Robert wrote above might be a workable solution too.. but I figured a different way out. I simply removed <includes> inside the maven-jar-plugin and it worked. I ran the build in eclipse by creating a build configuration and chose "debug" option. It spit out a lot of info about "configuration" which is otherwise not displayed.
Thanks!
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>path/to/final/folder</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
This worked!
Another approach is to set ouputDirectory to regular target/classes directory.
target/classes
So that unpacked classes plus your project classes will be avaialble in target/classes which can be bundled in to .jar using regular maven-jar-plugin by specifing **
Complete pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>a.b.c</groupId>
<artifactId>aaa</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>target/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<includes>
<include>**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven and emma plugin

How to configure maven build to fail if some of bundles to instrument are misspelled or no target/coverage/coverage.xml file report is generated?
I did not check, try:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-verifier-plugin</artifactId>
<version>1.0</version>
<configuration>
<verificationFile>target/coverage/coverage.xml</verificationFile>
</configuration>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

How to place the output jar into another folder with maven?

I'd like to place my output jar and jar-with-dependencies into another folder (not in target/ but in ../libs/).
How can I do that?
You can use the outputDirectory parameter of the maven-jar-plugin for this purpose:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>../libs</outputDirectory>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
But as cdegroot wrote, you should probably better not fight the maven way.
If you want to copy the artifact into a directory outside your project, solutions might be:
maven-jar-plugin and configure outputDirectory
maven-antrun-plugin and copy task
copy-maven-plugin by Evgeny Goldin
Example for the copy-maven-plugin is:
<plugin>
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
<execution>
<id>deploy-to-local-directory</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<skipIdentical>false</skipIdentical>
<failIfNotFound>false</failIfNotFound>
<resources>
<resource>
<description>Copy artifact to another directory</description>
<targetPath>/your/local/path</targetPath>
<directory>${project.build.directory}</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Another way would be maven-resources-plugin (find the current version here):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-files-on-build</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/[TO-DIR]</outputDirectory>
<resources>
<resource>
<directory>[FROM-DIR]</directory>
<!--<include>*.[MIME-TYPE]</include>-->
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I would do it this way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<copy file="target/${project.artifactId}-exec.jar" tofile="../../docker/${project.artifactId}.jar"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
This technique worked well for me:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
I specially like the solution using maven-resources-plugin (see here) because is already included in maven, so no extra download is needed, and also is very configurable to do the copy at a specific phase of your project (see here to learn & understand about phases). And the best part of this approach is that it won't mess up any previous processes or build you had before :)
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/dir/where/you/want/to/put/jar</outputDirectory>
<resources>
<resource>
<directory>/dir/where/you/have/the/jar</directory>
<filtering>false</filtering>
<includes>
<include>file-you-want-to.jar</include>
<include>another-file-you-want-to.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
Of course you can also use interpolated variables like ${baseDir} and other good stuff like that all over your XML. And you could use wild cards as they explain here
Maven dependency plugin is perfectly capable of copying all dependencies and just built artifact in a custom location. Following example will copy all runtime dependencies and a built artifact in a two execution phases.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/jars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/jars</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
While documentation of dependency plugin states in its documentation that in order to copy built artifact you have to use any phase after the package phase, that is not true if you are building jars. In that situation you can use package phase. At least in 3.3.0 version of plugin.

Resources