I have a java project done with Eclipse and Maven with this estructure folder:
enter image description here
Ok, when i make a Maven install to create the .jar take this structure folder:
enter image description here
So that the hierarchy is not the same and links to the images and css do not work.
I show you the code of pom.xml
enter code here
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.wepall
palle
0.4.0
com.thoughtworks.xstream
xstream
com.thoughtworks.xstream
xstream
1.4.9
palle
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.wepall.palle.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any ideas?
thanks a lot!!
Best regards
You should add a build section to your pom.xml:
<build>
<directory>${basedir}/target</directory>
<resources>
<resource>
<targetPath>${basedir}/target/resources</targetPath>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
Further Reference:
POM Reference (Build Section)
I think that you should create a war instead of a jar, because you are talking about css and images and jars should not contains that kind of files (see jar vs war).
In maven you only need to change the <packaging> of the project inside the POM.
Related
I have spent several hours on this and I'm unable to get it done. For a Maven/JavaFX-Application I am trying to get an executable jar. But whenever I build it with its dependencies and execute it with java -jar Kvn-jar-with-dependencies.jar I get one of these errors:
When executing the Fat Jar:
Error: Could not find or load main class com.qohelet.kvn.MainApp
When executing the Jar which apparently doesn't contain the dependencies
no main manifest attribute, in Kvn.jar
I use netbeans 8.2 for development (this is one of the last versions which can run with the OpenJDK 1.8.0) and I can run my project any time using (or better, Netbeans uses it):
cd /home/qohelet/Projekte/Kvn; JAVA_HOME=/home/qohelet/jdk1.8.0_111 /home/qohelet/netbeans-8.2/java/maven/bin/mvn "-Dexec.args=-classpath %classpath com.qohelet.kvn.MainApp 'USER' 1 'Indonesia'" -Dexec.executable=/home/qohelet/jdk1.8.0_111/bin/java org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
There had been an issue with the obtaining the dependencies due to the change by mvn to ssh, but I didn't even have maven installed before, it still worked. (Maven dependencies are failing with a 501 error), now one of the addresses in the project header contains a https instead of a http but I don't think this is much of an issue.
Currently the pom.xml looks like this, this is an edited version of https://maven.apache.org/plugins/maven-assembly-plugin/usage.html:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qohelet</groupId>
<artifactId>Kvn</artifactId>
<version>2.2</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<finalName>Kvn</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.qohelet.kvn.MainApp</mainClass>
</manifest>
</archive>
</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>
<dependencies>
<!-- Various -->
</dependencies>
<name>Kvn</name>
</project>
This version gives me the mentioned error.
I found a different suggestion where the -Section is set up like that which apparently worked for a while, but for some reason it doesn't seem to be advised as Netbeans complains:
Problem: Project Problem: Project's main artifact is processed through
maven-shade-plugin, resolvable by:
org.netbeans.modules.maven.problems.ProblemReporterImpl$MavenProblemResolver#a5534d
unresolved
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.qohelet.kvn.MainApp</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
A few months ago the project could still be built with the following -Section but this was mostly a result from trial and error until I had a working result:
<build>
<finalName>Kvn</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.qohelet.kvn.MainApp</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am not sure if I interpret this correctly but can it be due to version changes in Java?
SO provides a lot of answer, but I am not able to get anything to work.
https://stackoverflow.com/a/589111/2516892 gets me the exact same error:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.qohelet.kvn.MainApp </mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Similar to https://stackoverflow.com/a/4323501/2516892 with the following build section which doesn't return a Fat Jar but the same error.
What is the state of the art? How should it be done properly?
Thank you
Edit (1):
The structure of the Jar looks fine to me. 22Mb seems to be a reasonable size too. It a folder /com/qohelet/kvn/ which contains a MainApp.class
Using https://stackoverflow.com/a/574650/2516892 and compiling with
mvn clean compile assembly:single
weirdly results in a set of errors like
package javafx.beans.value does not exist
Which is odd as JavaFX is installed and I can run the program without any issue.
Netbeans is not doing much different:
cd /home/qohelet/Projekte/Kvn; JAVA_HOME=/home/qohelet/jdk1.8.0_111 /home/qohelet/netbeans-8.2/java/maven/bin/mvn clean install
But this results in a jar with only 254Kb, which I assume doesn't contain the dependencies. Including the make-assembly-part just creates the same error.
Using the 2nd one listed here https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html is just the same error: Error: Could not find or load main class com.qohelet.kvn.MainApp, the archive is slightly smaller (19Mb) and again it contains a folder-structure: /com/qohelet/kvn/ which contains a MainApp.class
when running at cmd normally command is java -jar xxx.jar param1.
my command is java -jar n2n-adaptor-cimbr-news-pdf-jar-with-dependencies.jar hostserver.xml
how to conficuration hostserver.xml at pom.xml? so tat no need run param at cmd.
<modelVersion>4.0.0</modelVersion>
<groupId>n2n</groupId>
<artifactId>n2n-adaptor-cimbr-news-pdf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
<main.class>com.n2n.newsPDF.MainNewsCIMBRHostServer</main.class>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<!-- We specify the Maven compiler plugin as we need to set it to Java
1.8 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
<suiteXmlFiles>
<suiteXmlFile>${project.build.directory}/resources/hostserver2.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<includes>
<include>hostserver2.xml</include>
</includes>
</resource>
</resources>
</build>
after i make configuration it can't running when i run java -jar n2n-adaptor-cimbr-news-pdf-jar-with-dependencies.jar.
please kindly advice.
Thanks
Sharon
This is a problem you need to solve within Java, not Maven. You define the parameters of your Java Main class. If you want to replace command line parameters with resources, this is the place to go.
I was trying to achieve a custom directory structure for my war with maven build.
Below is my build command used in pom.xml.
<build>
<finalName>abc</finalName>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<configuration>
<tasks>
<mkdir dir="bin" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>bin</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>bin</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
Please find below the current directory structure of war file after unzip the war. Maven is including complete webapp directory under WEB-INF/Classes , But i want only java class files(ndaws directory). I have tried a lot of excluding techniques, But nothing works.
Got the solution, My changes in pom is not reflecting, war is being created from build folder under target.
Thanks
remove resource directory from maven-resources-plugin
and add warSourceDirectory to maven-war-plugin
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
or if using intelij and webapp folder marked 'resoures', select the folder.
open context menu.
Mark Directory as > Unmark as Resources Root
I tryed alot of solutions. In the end I found one that solve my problem... It's a similiar case, but I need to exclude a folder inside the webapp.
I opened my war file using 7zip... The folder that I want to exclude were in the main page.
I used this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<packagingExcludes>myfolder/**</packagingExcludes>
</configuration>
</plugin>
Just change "myfolder" with the folder that you want to exclude.
If that is more than one folder, multiply this line.
Hi i am using maven buil to create an executable jar, i have few properties file. if i place the properties file in
src/main/resources
maven packages them inside the jar itself. I dont want this to happen, instead i want to place the properties file in a folder called conf and i want these properties file to be avalable to the jar during runtime.
The reason why this is because in future the user can have the flexibility to chnage a few property values like port number etc without.
i have pasted the pom.xml below
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hp.nfv</groupId>
<artifactId>DescriptorA</artifactId>
<version>1.0.0</version>
<name>DescriptorA/name>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.abc.Descripto</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
the property file i am using is 'utility.properties' which is present in the src/main/resources
i am using this in the java code as below
ResourceBundle locationUtilityProp = ResourceBundle.getBundle("utility", locale);
but when i execute the above pom.xml file, i get a jar file , which on running gives the below error
java.util.MissingResourceException: Can't find bundle for base name utility
when I un jarred the jar file, i found no .properties files in it.
I am completely new to maven so please can anyone help me make this jar pick the properties file from a directory structure other than src/main/resources at run time.
I got this requirement working , and below i have answered in detail so that it can help someone else someday
package structure:
you need to place .properties files inside src/main/resources when doing a maven build
src
|-main/java/com.abc/.java classes
|-main/resources/error.properties
after maven generates a jar file, then create a folder called config and copy all .properties files inside it. and place your jar file in same directory as your config folder as below
example
|-config
-error.properties
|-jar file generated by maven
code to fetch the resources files
static Locale locale = new Locale("en", "US");
static ResourceBundle locationUtilityProp =ResourceBundle.getBundle("error", locale);
pom.xml to create an executable jar file
first tell maven not to include any of properties files present inside src/main/resources
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
now include maven compiler plugin
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
now include the maven assembly plugin to create an executable jar.
in the mention the folder name where you want maven to pick the resource files( in my case error.properties)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.abc.hello</mainClass>
</manifest>
<manifestEntries>
<Class-Path>config/</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
If I got you right, you want to have a portable, configurable jar so you can edit the config without re-packaging. That can be achieved easily by providing the -cp (or -classpath) option with the path to the folder containing yout utility.properties. So the invocation looks like:
java -cp ./conf -jar youApp.jar
assuming that the config is in a conf sub-folder of where the main jar sits.
More on the classpath.
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>