How to include particular file from a folder in maven install target directory? - maven

Directory structure is
conf
axis2.aar
com
lib
META-INF
axis2.xml
build.xml
common-logging.properties
log4j.properties
*.class
I want that only axis2.xml should be included in axis2.aar in target folder when doining maven install. I have tried many include and exclude combinations but nothing is
working. After doing maven install, It is taking whole axis2.aar contents. I also tried filtering by setting it to true.Please suggest some solution.
Pom.xml
<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>CUDB_HSS</groupId>
<artifactId>CUDB_HSS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>wsdl</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>conf/axis2.aar</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-aar-maven-plugin</artifactId>
<version>1.6.4</version>
<extensions>true</extensions>
<executions>
<execution>
<id>cudb-hss-aar</id>
<phase>package</phase>
<goals>
<goal>aar</goal>
</goals>
<configuration>
<aarDirectory>conf/axis2.aar</aarDirectory>
<aarName>axis2</aarName>
<filesets>
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss</id>
<phase>package</phase>
<configuration>
<classifier>client1</classifier>
<finalName>cudb-hss</finalName>
<includes>
<include>com/accenture/**/*.class</include>
<include>macro_CUDB_Accenture_HSS.class</include>
<include>**/com/accenture/il/interfaces/cudb/hss/conf/jaxb/**/*.properties</include>
<include>**/com/accenture/il/interfaces/cudb/hss/error/bean/jaxb/**/*.properties</include>
<include>**/*.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>cudb-hss-wsdl</id>
<phase>package</phase>
<configuration>
<classifier>client2</classifier>
<finalName>cudb-hss-wsdl</finalName>
<includes>
<include>com/ericsson/**/*.class</include>
<include>**/com/ericsson/**/jaxb.properties</include>
</includes>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

The answer to your question can be:
<fileset>
<directory>conf/axis2.aar</directory>
<outputDirectory>target</outputDirectory>
<includes>
<include>axis2.xml</include>
</includes>
</fileset>

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.

How to unpack a war file copied from local directory using Maven Resources Plugin?

I am trying integrating CAS server with a Spring application. (I have configured CAS locally, in my Tomcat server and it is working properly). Using Maven Resources Plugin I copied the cas.war file to my Web app's target folder. But I need to unpack that cas.war file in order to overwrite some files that I have modified.
How can I unpack this war file which I have copied using Maven Resources Plugin`?
Here is my pom.xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>cas</warName>
<webResources>
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${maven-jetty-plugin.version}</version>
<configuration>
<webApp>
<contextPath>/cas</contextPath>
</webApp>
</configuration>
</plugin>
<!--Copy plugin-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<!--copy cas.war file-->
<execution>
<id>copy-initial-cas.war</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas</outputDirectory>
<resources>
<resource>
<directory>${CAS.LOCATION}/target</directory>
<includes>
<include>cas.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying deployerConfigContext file-->
<execution>
<id>copy-deployerConfigContext</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>deployerConfigContext.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying web.xml file-->
<execution>
<id>copy-web.xml</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>web.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<!--copying casdatabase.properties file-->
<execution>
<id>copy-casdatabase.properties</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/cas/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<includes>
<include>casdatabase.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--unpacking plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>unzip-war</id>
<phase>install</phase>
<configuration>
<tasks>
<echo message="unpack cas.war" />
<unzip src="${basedir}/target/cas/cas.war" dest="${basedir}/target/cas" overwrite="true"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
If you wish to override resources inside a war, you should be using the maven overlay method.

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>

I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

I dont get any error with this code. Just that the file that I want to exclude still gets added. I am using the maven plugin for eclipse
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>only</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>com.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<excludes>
<exclude>**/com/uiservices/controllers/*.* </exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
The maven-assembly-plugin doesn't work like that.
There what you want to do is override the configuration of the assembly descriptor jar-with-dependencies and that's not possible.
If what you want to do is create a jar similar to the one created by the assembly jar-with-dependencies but without some specific classes of your own project, you have to write your own assembly and call it in the maven-assembly-plugin like what follows.
Assembly in src/assembly/jar-with-deps-with-exclude.xml :
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<!-- TODO: a jarjar format would be better -->
<id>jar-with-dependencies-and-exclude-classes</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>${project.build.outputDirectory}</directory>
<excludes>
<exclude>com/uiservices/controllers/*.*</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
This will create an assembly with no the dependencies unpacked and with your classes added except the ones excluded.
And then in your pom.xml :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>only</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/assembly/jar-with-deps-with-exclude.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But if what you need is your classical jar without excluded classes, you can exclude them in the maven-jar-plugin directly :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>com/uiservices/controllers/*.*</exclude>
</excludes>
</configuration>
</plugin>
I had a similar issue with maven-assembly-plugin:
Project beans-conversion has files application.properties,
logback.xml, and logback.xsd
Project extract-conversion has also files called application.properties, logback.xml, and logback.xsd
Requirement is extract-conversion.jar should include beans-conversion.jar content but application.properties, logback.xml, and logback.xsd of extract-conversion.jar should override the beans-conversion.jar.
Solution:
We solved this using maven-shade-plugin as below in extract-conversion's pom.xml.
...
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>beans-conversion</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
...
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>logback.xml</include>
<include>logback.xsd</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</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>com.mycompany:beans-conversion</artifact>
<excludes>
<exclude>application.properties</exclude>
<exclude>logback.xml</exclude>
<exclude>logback.xsd</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
using maven-shade-plugin
ref: Maven dependency: exclude one class
<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>
<finalName>${project.artifactId}-jar-with-dependencies</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.taobao.arthas.core.Arthas</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:termd-core</artifact>
<excludes>
<exclude>META-INF/services/io.termd.core.spi.ConfigProvider</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

Resources