Move resource files to target and exclude from jar - maven

I have a maven project set up like this:
\src
\--main
\--java
\--fu
\--bar
\--AppMain.java
\--resources
\--log4j.xml
With a pom using the maven-resources-plugin to move my resource files to a target directory like this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>my/target/dir/config</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
And the maven-jar-plugin to create the jar like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>fu.bar.AppMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
When I run mvn clean install, my target location includes the compiled jar and the \config\log4j.xml. However, the jar also includes log4j.xml.
How do I exclude log4j.xml from the jar and still move it to \my\target\dir\config?

Adding this to my pom was the trick:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>

Related

Maven - How to create a POM that place files in the root

I need to create a pom.xml file that builds a jar with certain files place on its root. The pom.xml I have now creates 3 directories BOOT-INF, META-INF and org. Below is the build section. I want the files to be at the same level as the directories (BOOT-INF, META-INF, org).
sparc-jar.jpg
<plugins>
<plugin>
<groupId>oerg.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.sh</include>
<include>**/*.id</include>
<include>**/*.yaml</include>
<include>**/*.json</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.jar</include>
<include>**/*.js</include>
<include>**/*.map</include>
<include>**/*.html</include>
<include>**/*.hbs</include>
<include>**/*.cer</include>
<include>**/*.p12</include>
</includes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>**/*.yaml</include>
<include>**/*.json</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.cer</include>
<include>**/*.p12</include>
</includes>
</resource>
</resources>
</build>
This worked for me... Configure the spring-boot-maven-plugin as follows:
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
</configuration>

Maven does not copy resource files

I have a maven web application project that follows the maven standard directory layout so I have my resource files placed in src/main/resources/.
As this follows the standard I was expecting maven to automatically add the resource files to /WEB-INF/classes of the web application during the build but no files are copied.
I have to add the following lines to the build section of the pom file to get maven to copy the files:
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
My question is: is this supposed to be necessary?
I was wondering if any of the build section plugins I use could somehow interfere with the copying of the resources. Are there any plugins that are known to do this?
The build section of my pom file is here:
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>${tests.to.include}</include>
</includes>
<excludes>
<exclude>${tests.to.exclude}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<includes>
<include>**/ITSelenium*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
<executions>
<execution>
<id>add extra source directories</id>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add extra test directories</id>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>${project.basedir}/generated/src/test/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add integration test sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/it/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warName>res</warName>
<webResources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
</plugins>
</build>
Any help or information will be much appreciated.

Maven resource plugin expose an image from a different path

I have an immage accessible from /resources/gfx/loading.gif
I would like to have it accessible from /img/immage.gif
I tried with maven-resources-plugin with the following config, but actualy nothing happens.
I'm curretnly building the app from eclipse.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-loading-status</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/src/main/webapp/img</outputDirectory>
<resources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Assuming you are building a WAR, you could configure the maven-war-plugin to include additional web resources:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>/src/main/resources/gfx</directory>
<includes>
<include>loading.gif</include>
</includes>
<targetPath>img</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

Maven: File does not get excluded from build

I have a library maven module which has an application.conf file in /src/main/resources/ and I want to exclude it from the build in the pom of the module
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.conf</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
A maven module, which uses the above mentioned module, shall build a jar with all dependencies; I use the maven assembly plugin to build actually the jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<!--<descriptor>src/assembly.xml</descriptor>-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The file application.conf is still there in the jar. What I am doing wrong here ?
do this :
<exclude>**/application.conf</exclude>

Creating output dir structure with Maven

I have a java project XXX
src/main/java
src/main/config
src/main/scripts
I want output structure like
C:/target/XXX.jar
C:/target/scripts
I tried to use resource plugin but it is packing everything inside jar.
<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${artifactId}-${version}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
<resource>
<directory>src/main/scripts</directory>
<targetPath>/scripts</targetPath>
</resource>
</resources>
</build>
The maven-assembly-plugin is great for this sort of thing. You can create a descriptor something like this (in src/main/assemble/scripts.xml):
<assembly>
<id>scripts</id>
<formats>
<format>dir</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>/scripts</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Wire this into your build like so:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>src/main/assemble/scripts.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>copy-scripts</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This will bind the execution of the assembly to the package phase of the build.
This method will give you the flexibility to change how you package up the scripts in the future, or copy more resources in the build.

Resources