Maven pom looking for resources outside of jar - java

I'm having trouble creating an executable jar with all dependancies and resources packaged in it. So far I have everything inside the jar, but it is looking for the resources OUTside the jar.
Here is the structure of my project:
MyProject
----images
----resources
----src
----...
I'm able to Create the Executable Jar with dependancies packaged in, and the resources are packaged as well, however it is still looking for the resources in the "resources" folder and not IN the JAR.
The jar looks like this:
MyProject.jar
----images
----resources
----...
When I try run it, I get this error:
java.io.FileNotFoundException: .../MyProject/target/resources/default-style.xml (No such file or directory)
So my problem is this: my dependancies and resources are packaged in the jar like I want, but upon execution it's looking for "resources/" in the "target/" folder and NOT in the JAR.
Here's my pom:
<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>
<!-- MyProject INFO -->
<groupId>Main</groupId>
<artifactId>MyProject</artifactId>
<version>0.0.1-beta</version>
<name>MyProject</name>
<packaging>jar</packaging>
<!-- DEPENDANCIES -->
<dependencies>
<dependency>
<groupId>custom</groupId>
<artifactId>custom</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<!-- RESOURCES -->
<resources>
<resource>
<directory>src</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>${basedir}/images</directory>
<filtering>false</filtering>
<targetPath>images</targetPath>
</resource>
<resource>
<directory>${basedir}/resources</directory>
<filtering>false</filtering>
<targetPath>resources</targetPath>
</resource>
</resources>
<testResources>
<testResource>
<directory>src</directory>
</testResource>
</testResources>
<plugins>
<!-- For Java 6, you need to configure the maven-compiler-plugin. Add this to your pom.xml: -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- CREATE EXECUTABLE JAR WITH DEPENDANCIES -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main.Main</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>
</project>
EDIT: Looks like it might be my code instead. I'll take a look, thanks! (My company's proxy blocks most of stackoverflow so I can't reply to comments >.< That's why I'm writing it here. Thanks Jigar Joshi and Aurand!)

Look into the java build path. The default output folder for the resources might be incorrect. May be that is why resource contents are not copied properly.

Related

Unable to add resources to final jar for maven project in Intellij

I am unable to add images from my resources directory (which is marked as resources root) to the final jar that I want t generate. I have tried building a jar using File -> Project Structure -> Project Settings -> Artifacts -> Click green plus sign -> Jar -> From modules with dependencies. But failed. I have also tried building a jar from the Maven project window in Intellij. Which failed as well.
Here is my pom.xml file:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.project</groupId>
<artifactId>login</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<directory>
src/main/resources
</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.sample.project.Login</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
My project structure looks like this: Project structure
zip file of my sample project:
Archive
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>dependency:sources</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
Above code in pom.xml would copy xml files located in resources. You can change to some other files or remove
<includes>
<include>**/*.xml</include>
</includes>
will copy all files in resources.
The files that you have in the directory src/resources/ need to be moved to src/main/resources/, otherwise Maven will ignore them.
Additionally, remove this
<resources>
<resource>
<directory>
src/main/resources
</directory>
</resource>
</resources>
because this is the default convention that Maven will use anyway.

Maven - maven-war-plugin to generate jar and install it to maven local repository

I am using maven-war-plugin in my pom.xml to generate a jar file in parallel with my war file in an java web project build. My maven build is creating war and jar files in the target directory. And only war file is installed to local repository. Is there a way to push the jar file created as well to local repository. Below is the snippet of my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<archiveClasses>true</archiveClasses>
<packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
<webResources>
<resource>
<directory>${project.basedir}\src\main\webapp</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
Thanks in advance!
pom.xml content:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>x</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>x</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<archiveClasses>true</archiveClasses>
<packagingExcludes>WEB-INF/lib/x*.jar</packagingExcludes>
<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>
<webResources>
<resource>
<directory>${project.basedir}\src\main\webapp</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/webapp/WEB-INF/lib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is not the right way to use maven. Maven is all about modularity and as a consequence there is a "one project - one artifact" rule (or recommendation). See also this blog if I can't convince you : How to Create Two JARs from One Project (…and why you shouldn't) . It is about multiple jars but the concept is the same.
I think you should restructure your work into having one separate project for the jar, while the others use it as a dependency.

How to add resources to jar using maven shade plugin

My Project structure has resources folder inside the src/main/ folder. The resources folder contains the file server.properties. My pom is as follows:
<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.fde</groupId>
<artifactId>Listener</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Listener</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>3.6.10.Final</hibernate.version>
<java.version>1.6</java.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Hibernate dependencies START -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Hibernate dependencies END -->
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fde.ListenerServer</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>resources</resource>
<file>server.properties</file>
</transformer>
</transformers>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The Jar is created properly and the main class is mentioned in the manifest. I have the following questions :
The target folder contains the classes folder whch has the class files. The jar also contains them so why are they needed. My goal is to have a executable jar with all the dependancies only.
The resources are not getting added in the jar at all. I have added the transformer according to instructions seen on the net but no use!!!
What are the other dir getting created in the target folder (maven-archiver, surefire, surefire-reports etc) ??
Another jar gets created every time i do a maven clean install (original-Listener....jar)
I have absolutely no clue about how to include resources. Any help is appreciated!!
EDIT:::
This is the tag i used for the maven-assembly-plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.fde.ListenerServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
This created the Listener-1.0-SNAPSHOT-jar-with-dependencies.jar with all the classes from referred jars in the folders. The manifest also contains the main class.
The problem still is that I cant include the resource bundle in the folder \src\main\resources.
Also I cant understand why jar files referenced from my code are included in the jar as well as inside the META-INF folder.
The problem wasn't in your maven-shade-plugin's configuration, rather that you have explicitly set resource directory to a wrong path:
<!-- wrong -->
<resource>
<directory>resources</directory>
</resource>
As <directory> element's doc says: "Describe the directory where the resources are stored. The path is
relative to the POM."
So if you follow default maven project structure you have to set like this:
<!-- correct -->
<directory>src/main/resources</directory>
or don't set it, then it falls back to same as maven defaults.
I removed the following from the pom.xml and the property files were included at the root dir.
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
<testSourceDirectory>src/test/java</testSourceDirectory>
Still havent figured out why there is a repetition of the referred classes in the jar.

How can I include resources (css, js) from jar using maven assembly?

I have a project which contains shared resources for several sub-projects; i.e. as well as Java code, it contains css and javascript.
My child sub-projects are packaged as jars using the maven assembly plugin. For some reason these do not include the css and js resources from the parent library.
So my question is simple: how can I configure the projects so that this happens? I don't want the child projects to be wars; they are dropwizard (/jetty) projects that run as standalone jars.
EDIT - I should make explicit that the resources are included in the parent project's jar; it's only when assembly (on a child project) includes the jar that they somehow go missing.
Parent pom:
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.parentproject</groupId>
<artifactId>parentproject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
...
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
Example child pom:
<?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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.child</groupId>
<artifactId>child</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Child</name>
<dependencies>
<dependency>
<groupId>io.parentproject</groupId>
<artifactId>parentproject</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-proc:none</compilerArgument>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>io.childproject.SomeClass</mainClass>
</manifest>
</archive>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</project>
So my question is simple: how can I configure the projects so that this happens? I don't want the child projects to be wars; they are dropwizard (/jetty) projects that run as standalone jars.>
You need to add resource directory in <resource></resource> so that it picks up while building jar
You could either use maven-assembly plugin or configure your pom.xml to have these resources files specified as maven resources, assembly plugin will give you much more flexibility
I ended up using maven shade instead. It's a little more heavyweight than assembly but it does the job.
Still, if anybody knows how to make assembly do this please add an answer.

Changes in source files of Maven project not reflected in tomcat

My maven project is in final stage. I have deployed my war file to running tomcat instance by using mvn tomcat7:deploy command and the war file is exploded in the webapps folder. I can access all the source files in the war file through url. But the problem is, when i change anything in my source files ( jsp and servlet ) the updated war file is not reflected in the running tomcat server.
my pom.xml is as follows.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ceino.maven</groupId>
<artifactId>MavenWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>MavenWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>MavenWeb</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://127.0.0.1:8080/manager/text</url>
<!--<warFile>/home/shebin/Maven Folder/MavenWeb/target/MavenWeb.war</warFile>-->
<server>TomcatServer</server>
<path>/MavenWeb</path>
<username>tomcat</username>
<password>tomcat</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
Am using maven 3.0.4 and tomcat 7.0.35. Pls help.
Try to call mvn clean install every time before running mvn tomcat7:deploy
If you still have the problem, try mvn tomcat7:undeploy before. The undeploy removes the old war file und the deploy try to push the new one
I used to have the same problem and I solved it adding this to the pom.xml file:
Here I'm synchronising 2 folders:
src/main/webapp/resources -> target/m2e-wtp/web-resources/resources
src/main/webapp/WEB-INF/views -> target/m2e-wtp/web-resources/WEB-INF/views
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/m2e-wtp/web-resources/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-views</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/m2e-wtp/web-resources/WEB-INF/views</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF/views</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

Categories

Resources