how can set both classpathPrefix and mainClass in pom? - maven

I have pom.xml like this:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>package.mainClass</mainClass>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
and I compile and package my project successful. but when I java -jar myProject.jar I get Error: Could not find or load main class package.mainClass!!
I think it happened because I wrote both mainClass and classpathPrefix in manifest! what is the way to set them with each other in pom.xml?

"Main-Class: first.PathGetter" instead of "Main-Class: package.mainClass" in pom.xml file, according to your MANIFEST.MF

Related

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

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>

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

Maven: How do I mark a jar as sealed?

This sounds so simple, so why an I struggling?
Is there a POM entry I can use to seal all the packages?
Or do I provide a manifest with value 'Sealed: true'?
Thanks
Jeff Porter
> Or do I provide a manifest with value 'Sealed: true'?
As explained in "Manifest Customization":
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifestEntries>
<Sealed>true</Sealed>
</manifestEntries>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>

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

No manifest file from maven build

I'm using Maven to generate a war file. I'm trying to get it to generate a manifest file in the war. Right now it's not happening. I've included the following in my pom.xml, but I can't get it to output a manifest file with that information. Anyone have any ideas or pointers? There is no MANIFEST.mf being put into the war.
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
...
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
You should add this to your plugin config:
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>

Resources