I have a maven project with a module which is a java web application (A module) and another module (B module) that is also a java web application that uses the classes from the A module and its resources. Creating a jar and installing it in the maven repository its no problem to compile the B module but my problem its to copy the resources from A to B.
I am trying to copy the jsp and other files from A module. Following the documentation and some blogs I managed to get the target\classes\META-INF\maven\remote-resources.xml, but when I try to copy those resources in the B module I only get the folder structure but any file inside.
A module pom
<profile>
<id>validator</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<resourcesDirectory>${project.basedir}/src/main/webapp</resourcesDirectory>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
<includes>
<include>**/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
B module pom
<profile>
<id>embedded</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.4</version>
<configuration>
<resourceBundles>
<resourceBundle>my.group:my.artifact:${my.version}</resourceBundle>
</resourceBundles>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/config/embedded</directory>
</resource>
</resources>
</build>
</profile>
In the output I get no errors but there are no files in the target\maven-shared-archive-resources\ only the folder structure as it is in module A.
Any idea what I am doing wrong?
Ok, I found a solution.
Looks like remote maven-remote-resources-plugin only works for resources under src/main/resources so in my case the trick was to copy /webapp/* to that folder via resources and to execute the plugin after the resources are copied I had specify <phase>process-resources</phase> in the plugin because otherwise it was been executing before having the resources in their place.
Here is the code for the profile
<profile>
<id>share-resources</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/config/embedded</directory>
</resource>
<!-- This is the one for webapp -->
<resource>
<directory>${project.basedir}/src/main/webapp</directory>
<targetPath>${project.build.outputDirectory}/shared-resources</targetPath>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>bundle</goal>
</goals>
<configuration>
<resourcesDirectory>${project.build.outputDirectory}</resourcesDirectory>
<excludes>
<exclude>**/WEB-INF/**</exclude>
<exclude>**/META-INF/**</exclude>
</excludes>
<includes>
<include>**/shared-resources/**/*.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
To retrieve the resources in module B just did as in the documentation
Related
This question already has answers here:
Building a fat jar using maven
(7 answers)
Closed 1 year ago.
I'm trying to build a fat jar with maven assembly plugin for distributing a desktop application.
My POM looks like
<build>
<!-- To parse properties files under resources folder : -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.acme.qpguard.editor.Application</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>
The command mvn assembly: single is creating a fat jar with other dependencies, but fat jar does no include my classes.
So while starting the jar, I'm getting an error
Error: Could not find or load main class com.acme.qpguard.editor.Application
How can I fix my POM so that it includes my project files too
Please note that the project is running fine in Eclipse.
Thanks, #Mayur and #Randy Casburn. Your pointers definitely helped me in finding a fix using maven shaded plugin.
I'm posting the fix as someone may find this useful at a later point of time
<build>
<!-- To parse properties files under resources folder : -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.acme.qpguard.editor.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note: I have to apply a filter for removing signature files from some jars as it was breaking the execution.
Below is my project structure.
I require to copy a file from the location \MainProject\src\non-packaged-resources to the \MainProject\sub-project2\sub-project2-web\src\main\resources location on building the MainProject.
Below is the build section of the pom file of my sub-project2-web but the file is not copied while building.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When observing the build log, it skips the file being copied with the below error
skip non existing resourceDirectory
But the maven build gets successful.
Goal is to bind the file which exists inside the \MainProject\src\non-packaged-resources to all the ears (sub-project1.ear, sub-project2.ear) on building the maven project. First I am trying to test this in the sub-project2-web only.
Please advice on how to provide correct paths based on this requirement.
I am using maven 2.2.1 version (project is built on this.)
Define the paths with reference to your project base directory as follows.
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<directory>${project.basedir}/src/non-packaged-resources</directory>
Then add it in the pom.xml file in the maven-resources-plugin.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/sub-project2/sub-project2-web/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Based on the above answer I found the correct way to get the locations of the parent and the base directories. Below worked for me.
<build>
<finalName>sub-project2-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>**/.copyarea.db</warSourceExcludes>
<packagingExcludes>**/.copyarea.db</packagingExcludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/src/main/resources</outputDirectory>
<resources>
<resource>
<directory>${project.parent.parent.basedir}/src/non-packaged-resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
I need to be able to use "variables" for a maven plugin like this:
<plugin>
<groupId>com.jelastic</groupId>
<artifactId>jelastic-maven-plugin</artifactId>
<version>1.8.4</version>
<configuration>
<api_hoster>${api_hoster}</api_hoster>
<email>${email}</email>
<password>${password}</password>
<environment>${environment}</environment>
<!--<context>[specify the context if you need it]</context>-->
<!--<comment>[insert comment if you need it]</comment>-->
</configuration>
</plugin>
Already have the properties file set in the base directory and have used the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/jelastic.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Still, the variables in the plugin cannot be resolved, what could be wrong here?
You must either declare those variables as <properties> in project or profile or pass them as env variables like mvn whatever -Dyourprop=value
Read about properties: https://maven.apache.org/pom.html#Properties
Read about profiles: https://maven.apache.org/guides/introduction/introduction-to-profiles.html
add those lines to your POM:
<build>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
And this properties file:
api_hoster:${api_hoster}
email:${email}
password:${password}
environment:${environment}
and invoke any maven plugin by mentioning a profile:
mvn clean -Pname
i have this structure in my maven project
src
|
|_java
|_resources
|_webapp
|
|_addin
|
|_fckconfig.dev.js
|_fckconfig.js
|_fckconfig.prod.js
my pom.xml:
<properties>
<webXmlfolder>default</webXmlfolder>
<profileVersion>defaultVersion</profileVersion>
<majorVersion>2</majorVersion>
<minorVersion>50.3-0</minorVersion>
</properties>
<profiles>
<profile>
<id>dev</id>
<properties>
<profileVersion>DEV</profileVersion>
<webXmlfolder>dev</webXmlfolder>
</properties>
</profile>
<profile>
<id>preprod</id>
<properties>
<profileVersion>PREPROD</profileVersion>
<webXmlfolder>preprod</webXmlfolder>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileVersion>PROD</profileVersion>
<webXmlfolder>prod</webXmlfolder>
</properties>
</profile>
</profiles>
<!-- FIN Profiles -->
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<includes>
<include>**/*.${webXmlfolder}.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp/addin</directory>
<filtering>true</filtering>
<includes>
<include>/addin/*.${webXmlfolder}.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-web.xml</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/config/${webXmlfolder}/WEB-INF</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
when i execute mvn clean install -P prod i want to have just:
src
|
|_java
|_resources
|_webapp
|
|_addin
|
|_fckconfig.prod.js
but there is nothing going on I always file now, why?
As exaplained for maven war plugin , src/main/webapp is default folder for web application source. So, all file in src/main/webapp will be automatically added to war. Try to move addin folder outside src/main/webapp.
If you couldn't move folder, you need to work with 'exclude' tags as exaplained here.
I suggest you take advance of the maven-war-plugin to configure the contents of your WAR file based upon your profiles:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-war</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<webXml>src/main/config/${webXmlfolder}/WEB-INF/web.xml</webXml>
<warSourceIncludes>*.*</warSourceIncludes>
<warSourceIncludes>addin/*.${webXmlfolder}.js</warSourceIncludes>
</configuration>
</execution>
</executions>
</plugin>
By the way, I notice you have one possible incompatibility about the copy-web.xml plugin if you execute it at the package phase, because the war plugin always executes in that same phase, too:
Either move the copy-web.xml to an early phase.
Either take advance of the <webxml> node from the maven-war-plugin, as I have included in my scriptlet... But assuming your web.xml files do not need to be filtered. (Do they?)
I am trying to build a groovy project using maven. My packaging type is war file. Maven is building the project and putting all dependent libraries in WEB-INF/lib folder but it is compiling all code into class files and putting it into WEB-INF/classes folder. Is there a way I can tell maven to build jar file for my project also and put it into WEB-INF/lib folder.
My pom.xml looks like this :
<groupId>com.myproject</groupId>
<artifactId>ExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Example Project</name>
<url>http://maven.apache.org</url>
<dependencies>
...
...
...
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/groovy</directory>
<excludes>
<exclude>**/*.groovy</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
<source>src/main/resources</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
<source>src/test/resources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>master</finalName>
</build>
In these scenarios the usual approach is to you separate your library code in a different module that will be a dependency from your war module. For this suggestion you can see also how to generate jar and war both in project .
However if you still prefer to go with the solution you mention, you can do it with the following configuration in your pom
<configuration>
..
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
</configuration>
(see http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html, and how to use class file from another war)