Maven add my own information during build to MANIFEST.MF file - maven

I need your help. I have to write a script adding versions of sub-projects to MANIFEST.MF file during build application by Maven.
I write some Mojo class that gets information I need (I named it GenerateVersionPropertyFile.class). Now I need know how to put this information to manifest file.
Here is my pom.xml:
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
<!--
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
-->
<manifestEntries>
<Built-By />
</manifestEntries>
<manifestSections>
<manifestSection>
<Name>appName</Name>
<manifestEntries>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
<Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
</manifestEntries>
</manifestSection>
<manifestSection>
<Name>exampleApp</Name>
<manifestEntries>
<Implementation-Title>exampleEntry</Implementation-Title>
<Implementation-Version>dupa2</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
<Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>maven.GenerateVersionPropertyFile</mainClass>
<arguments>
<argument>${project.basedir}\src</argument>
<argument>${project.build.directory}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>server-ejb-build-devel</id>
<activation>
<property>
<name>build-devel</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<archive>
<manifestSections>
<manifestSection>
<Name>app1</Name>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
<Implementation-Timestamp>${timestamp}</Implementation-Timestamp>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Make sure, if you are adding custom properties in MANIFAST.MF file which build package is JAR/WAR.
if packaging JAR in your pom.xml file then use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<mode>development</mode>
<url>http://testing/</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
OR if packaging WAR in your pom.xml file then use
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<mode>development</mode>
<url>http://testing/</url>
</manifestEntries>
</archive>
</configuration>
</plugin>

Have to use the maven-jar-plugin on your pom.
Example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<key>value</key>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>${maven-ejb-plugin.version}</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>

Related

Error: Could not find or load main class while executing jar

I have used maven assemble plugin to generate executable jar with dependencies.
I am trying to run the jar and getting Error: Could not find or load main class.
I have tried with and also tried option to specify classes folder.
Here is my pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.abc.TestApplication</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Try to use configuration block inside of an execution one:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.abc.TestApplication
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>

buildnumber-maven-plugin does not resolve its ${buildNumber} property

My trouble is here:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
<manifestEntries>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${buildNumber}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
What I'd like to do is to get the build number in runtime through manifest properties. There are plenty of instructions on how to use buildnumber-maven-plugin but I don't have a clue why in my manifest ${buildNumber} variable is not resolved.
<scm> tag is present in POM.
When building I see the following message:
Storing buildNumber: a391951e51a593c5267a77264938d982696ed65a at timestamp: 1426368059203
Still here is the result:
Implementation-Version: ${buildNumber}
Any help is much appreciated. Thank you.

maven build jar with test resources file only

I have a project which will be built has jar.
structure of project as below
> src | - main
> |- java
> - resources
> | - test
> | - resources
so now i want to create 2 jar files using maven
1) main/java and main/resources
2) main/java and test/resources
My POM
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.tool.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You never specified what is happening for you that is not what you're after, but looking at your example, it looks like you've got two different versions of the same plugin defined in your POM to try and hack what you want.
You can do what you're after by using the <execution> tag to define the desired behavior.
<plugin>
<!-- jar plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<!-- use these to define different execution tasks during the package phase -->
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${project.mainClass}</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>test-jar</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix> <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
After running this, you should have two jar's in your output directory, one named [your artifactID]-[version].jar and one named [your artifactID]-[version]-tests.jar

How to include custom files in JAR with Maven assembly plugin jar-with-dependencies

I need to include custom file (/com/app/log4.properties) in a final JAR.
How can I add one file to my JAR when using jar-with-dependencies?
Now there are only class files in that JAR. I'm using:
mvn assembly:assembly
My pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.app.Start</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>
</plugins>
</build>
Thank you.
Put non-Java files that need to be in the final artifact in src/main/resources, in this case:
src/main/resources/com/app/log4j.properties

Packaging specific dependent jars with Maven

My maven project has several dependent jars, but when I create a jar of my project I would like to include a subset of those dependencies. Is there a way to do this? Currently, I am using the pom.xml below (from question How can I create an executable JAR with dependencies using Maven?), but it is packaging every dependency with my project.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Try:
<dependencySets>
<dependencySet>
....
<excludes>
<exclude>commons-lang:commons-lang</exclude>
<exclude>log4j:log4j</exclude>
</excludes>
</dependencySet>
....
</dependencySets>
See Including and Excluding Artifacts for more details

Resources