maven assembly plugin and spring config xml - java

The created jar does not find the configuration file
unable to locate spring namespacehandler for xml schema namespace
main
ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");
pom
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
enter image description here

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>Main.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>

Related

How to append folder to each classpath entry using maven dependency and jar plugin?

I am trying to append folder before for all classpath entries in manifest file.
As you can see in pom file below, I am using jar plugin to add classpath entries to manifest.
With dependency plugin I am copying dependencies to ${project.build.directory}/${finalName}-lib folder.
I want to keep these dependencies in that folder and change classpath entries to match ${finalName}-lib folder path.
pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>${jarpluginoutput}</finalName>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>desktop.core.Main</mainClass>
</manifest>
</archive>
<excludes>
<exclude>log4j2-test.properties</exclude>
<exclude>**/.keep</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>compile</includeScope>
<outputDirectory>${project.build.directory}/${finalName}-lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Solution is made by adding <classPathPrefix>.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>${jarpluginoutput}</finalName>
<archive>
<manifest>
<classpathPrefix>${finalName}-lib</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>desktop.core.Main</mainClass>
</manifest>
</archive>
<excludes>
<exclude>log4j2-test.properties</exclude>
<exclude>**/.keep</exclude>
</excludes>
</configuration>
</plugin>

Why does it still show "no main manifest attribute, in..."

I'm pretty new to Maven and watched some Youtube videos about it, so I have done some steps of them for example to chose the <mainClass>. However it still shows:
no main manifest attribute, in...
if i want to start the file with java -jar.
I can show you my plugin's paragraph:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.testpackage.testclass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>repackage</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
I have added a new plugin paragraph so then i worked:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>zaehler.test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Run maven web application from command line

I'm trying to run my maven web application through:
java -jar CliniKeyMaven-1.0-SNAPSHOT.war
First, I faced the error no main manifest attribute in CliniKeyMaven-1.0-SNAPSHOT.war and after searching for a quite long time I reached this in my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<attachClasses>true</attachClasses>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.clinikeymaven.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
and this is the App.java that contains a main:
package com.mycompany.clinikeymaven;
public class App {
public static void main( String[] args ) {
System.out.println( "Hello World!" );
}
}
But I'm now facing the error:
Could not find or load main class com.mycompany.clinikeymaven.App
When I run the application from the NetBeans it is working but I need to run it from the command line.. what should I do?
this is a working pom .Pls try this one.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.clinikeymaven.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
You need to user the Maven jar plugin and not the Maven war plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.clinikeymaven.App</mainClass>
</manifest>
<compress>true</compress>
<index>true</index>
</archive>
</configuration>
</plugin>
Also as people said in the comments, with the maven-war-plugin you are creating a .war file that needs an application server to be deployed on in order to run.
For example the generating .war could be deployed on in tomcat server.
Try
java -cp CliniKeyMaven-1.0-SNAPSHOT.war com.mycompany.clinikeymaven.App
Alternately, you can update the content of your manifest file to following:
Main-Class: com.mycompany.clinikeymaven.App
Then you can try running it the following way:
java -jar CliniKeyMaven-1.0-SNAPSHOT.war

How to put all dependencies in separate folder for runnable jar?

I'm using mvn package to create a runnable jar with all dependencies packed inside, which runs fine.
But I'd prefer to have all external dependencies packed in a separate folder. What would I have to change therefore?
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>my.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Use the maven-dependencies-plugin to specify an output directory for the copy-dependencies execution.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<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>
Update:
To let the jar know where to find the lib folder, you can specify this as a Class-Path value in the manifest using the maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>foo.bar.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Hope this helps.

Different MANIFEST.MF for default jar-file and tests.jar

I'm trying to create different MANIFEST.MF files for the jar-packaged artifacts and the test-jar-packaged. The maven-jar-plugin being used to add additional stuff into the MANIFEST.MF - that works perfectly so far. But if I'd like to chose different template file for the MANIFEST.MF for the testproject, Maven only uses the second referenced template for both artifacts...
How can I get Maven to use the PROD-MANIFEST.MF-template for the normal jar-packaging and the TEST-MANIFEST.MF-template for test-jar-packaging?
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-manifest-mf</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestFile>foo/TEST-MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-manifest-mf</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestFile>foo/PROD-MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
Wrap each plugin configuration you have provided in a profile.
<profiles>
<profile>
<id>PROD</id>
<build>
<plugins>
// your PROD plugin configuration
</plugins>
</build>
</profile>
<profile>
<id>TEST</id>
<build>
<plugins>
// your TEST plugin configuration
</plugins>
</build>
</profile>
</profiles>
Then you invoke Maven with a profile
mvn package -P PROD
Hope that helps.
Try this:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>test-manifest-mf</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestFile>foo/TEST-MANIFEST.MF</manifestFile>
</archive>
</configuration>
</execution>
<execution>
<id>default-manifest-mf</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestFile>foo/PROD-MANIFEST.MF</manifestFile>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
This configuration is performing 2 different executions of the same plugin, each of which has its own archive configuration.
If there is a parent pom somewhere in your hierarchy that has the archive configured outside of an execution, like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
... other archive config ...
</archive>
</configuration>
</plugin>
then that configuration will be merged with what you have by default. If you don't want that to happen, add the combine.self attribute to the <archive> element like so:
<archive combine.self="override">
as described in the plugins section of the POM reference.

Categories

Resources