Maven war project copy website into webapp directory - maven

I have a maven java web project and it build fine, and I have another website project (pure HTML/Javascript) which is outside the maven web project.
The directory structure is like:
project
|--backend
|--|--src
|--|--|--main
|--|--|--|--java
|--|--|--|--webapp
|--|--pom.xml
|--frontend
|--|--test
|--|--app
What I want to achieve is during the build in maven, copy the frontend/app directory into the webapp directory, before the WAR file is produced. How can I do that in maven?

Actually the maven war plugin support this directly.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>../website</directory>
<include>app/**/*</include>
</resource>
</webResources>
</configuration>
</plugin>

use a maven plugin such as maven resource plugin

Related

IntelliJ: Copy war file after build

I'm using IntelliJ + Maven to generate war files.
The war is always generated in ProjectDirectory/target/projectname-version.war
After the build process is done, I want to copy the generated war file into a different location (something like cp output X:/remote/tomcat_webapps/projectname.war).
I already tried to configure the directory where maven builds the project (within the pom.xml). However, maven always deletes the containing folder and all its contents, so that is not an option.
How can I automatically copy the generated war file into a different location?
you can modify the maven war plugin in your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>X:/remote/tomcat_webapps</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
I'm not quite sure, if it is the outputDirectory or should it be webappDirectory, like in the documentation
https://maven.apache.org/plugins/maven-war-plugin/usage.html

How to make the created JAR file from Maven-WAR plugin into build path

I have used Maven -war plugin to create a JAR from another reference WAR .Now this JAR is available in the target folder.
My problem is I have to use the class which is available inside the created JAR in target folder but I am unable to get the class from JAR.
Is it possible to make available the created JAR available in the classpath through POM.
My maven WAR plugin as like below.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay />
<overlay>
<groupId>test.Vehicle</groupId>
<artifactId>test-location</artifactId>
</overlay>
</overlays>
<attachClasses>true</attachClasses>
</configuration>
</plugin>

Maven: include folder in resource folder in the war build

I've a folder named extra-jars in the src/main/rescource, but how can I include these in the build? I want them to be put in the lib folder with the rest of the jars. I tried including them through , but that didnt work.
For jars that are not distributed by a Maven repository, the simplest way is place the extra jars in the src/main/webapp/WEB-INF/lib directory of your project. Maven will by convention, include everything under the src/main/webapp in the final war artifact.
An additional method is to use the Maven War Plugin. It has the ability to add additional files to the final war artifact though plugin configuration.
In the build section of the POM add something like the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resource/extra-jars</directory>
<includes>
<include>*.jar</include>
</includes>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The <configuration> section is the key to adding additional files.
The <directory> element defines the source location of the resource. The path is relative to pom.xml.
The <includes> element defines what files found in the above directory to include.
The <targetPath> element defines the destination directory in the WAR to which the files are copied.
These jars should be added as Maven dependencies, not by copying them into the lib folder. This is the sort of thing Maven is designed for.

maven-jar-plugin, include upper dir

Build part of POM
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<classesDirectory>./</classesDirectory>
<includes>
<include>*.wsdl</include>
<include>*.xsd</include>
<include>sources/</include>
<include>../configuration.doc</include>
</includes>
</configuration>
</plugin>
In target jar I have all wsdl from root dir, xsds from root dir and sources dir.
But no configuration.doc file in jar.
Any ideas?
You should move the configuration.doc into the appropriate directory like src/main/resources.
How about adding the relevant folder from where you want the .doc to be picked up to your project pom using maven resources plugin - specifically the <resources> configuration - there are examples here. This will make the contents available to the jar plugin.

How to include resources from war to another maven project

I have a maven project , which needs to copy webapp/WEB-INF/ resources from another maven project which is packaged as a war .
How do I do it ?
PLease suggest
As Bittrance said, you should use the maven dependency plugin.
The better way is to create project that include all your shared resources, probably a type zip, which is build up with the assembly plugin. This is the good "maven way". It's a better solution than unpacking a war.
Then, refer it
<dependency>
<groupId>com.mygroup/groupId>
<artifactId>my-dependencies</artifactId>
<version>1.0.0</version>
<type>zip</type>
</dependency>
Next, you use the maven dependency plugin to unpack your resources, in the directory of your choice (probably WEB-INF/ ?)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-cfg-test-resources</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>resources</phase>
<configuration>
<outputDirectory>${project.build.directory}/WEB-INF/</outputDirectory>
<includeArtifacIds>my-resources</includeArtifacIds>
<excludeTypes>pom</excludeTypes>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
I'm not realy sure of this code snippet (written for another purpose), but this is an example.
For more information, please follow this link : http://maven.apache.org/plugins/maven-dependency-plugin/
If you can't shared a common-project including your files, you can unpack war including only ftl (or whatever you want), but it's not a realy clean solution ;)
There is a lot of posts that deal with this subject :
Unzip dependency in maven
...
Just try with the keywords maven-dependency-plugin, unpack :)
Hope that will help you.
I can see some alternatives:
Use external references in your version control system to point all repos to the same files.
The Maven Dependency module can copy and unpack project dependencies. From there, you can use the Maven Assembly plugin (or Ant targets) to include parts of that dependency in your own installation.
At least for the FTL files, perhaps you could package them in a separate Jar file and then load them as resources through the class loader.
If the resources are filtered, you may get into problem with solution 1 if you want the filtered version and 2, 3 if you want the source version.
Hope this helps.
(This assumes your dependent project is java (jar) and not another web app, if it is a webapp I think the solution is similar).
I suggest a (slightly) different approach:
Instead of reading resources from war, add this to your war pom, to generate a jar in the artifact as well as a war:
<!-- maven war plugin config -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<configuration>
...
<attachClasses>true</attachClasses>
<classesClassifier>some-string</classesClassifier>
</configuration>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
...
<resources>
<!-- This is for inclusion in the jar, so dependent module can load it -->
<resource>
<targetPath>some-path</targetPath>
<directory>src/main/webapp/path...</directory>
<includes>
<include>your-resource</include>
</includes>
</resource>
</resources>
And this to your consuming pom, so the generated jar will be loaded:
<dependency>
<groupId>com.company</groupId>
<artifactId>...</artifactId>
<classifier>some-string</classifier>
</dependency>
Then you will be able to load the resources the usual way (getResourceAsStream("some-path/your-resource"))

Resources