Renaming jar of the same Maven project before bundling it into its war file - maven

We've a Maven project that has war packaging. It has a couple of other projects as dependencies. The requirement here is that all projects added as dependencies should have a common naming convention such that they can be patched in every release. So we decided to replace the version from all such artifacts by -1.0-SNAPSHOT. The below code does it well for artifacts added as dependencies.
We want the classes of this project itself to be included as a jar file. So we set archiveClasses to true. Now the problem here is that the jar generated out of this has the version appended to it - ns-commonservices-6.5.x and maven-dependency-plugin is unable to rename it to ns-commonservices-1.0-SNAPSHOT (Hence, I've removed that code).
Is there any way by which we can rename the jar/artifact of the same project before bundling it into its own war?
Kindly refer the screenshot below. In this we want ns-commonservices-6.5.x.jar to named as ns-commonservices-1.0-SNAPSHOT.jar.
<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.ns.commonservices</groupId>
<artifactId>ns-commonservices</artifactId>
<packaging>war</packaging>
<version>6.5.x</version>
<name>NSHub</name>
<properties>
<nsweb.version>6.5.x</nsweb.version>
<maven-compiler-plugin.version>3.2</maven-compiler-plugin.version>
<buildDirectory>${project.basedir}/target</buildDirectory>
</properties>
<prerequisites>
<maven>3.2.5</maven>
</prerequisites>
<dependencies>
<dependency>
<groupId>com.ns</groupId>
<artifactId>ns-core</artifactId>
<version>${nsweb.version}</version>
</dependency>
<dependency>
<groupId>com.ns</groupId>
<artifactId>ns-common</artifactId>
<version>${nsweb.version}</version>
</dependency>
</dependencies>
<build>
<directory>${buildDirectory}</directory>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>Cp1252</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archiveClasses>true</archiveClasses>
<attachClasses>true</attachClasses>
<warSourceIncludes>WEB-INF/**</warSourceIncludes>
<packagingExcludes>
WEB-INF/lib/ns-common-${nsweb.version}.jar,
WEB-INF/lib/ns-core-${nsweb.version}.jar,
WEB-INF/classes
</packagingExcludes>
<webResources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xls</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.version</include>
<include>**/*.json</include>
</includes>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.ns</groupId>
<artifactId>ns-common</artifactId>
<version>${nsweb.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${buildDirectory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<destFileName>ns-common-1.0-SNAPSHOT.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>com.ns</groupId>
<artifactId>ns-core</artifactId>
<version>${nsweb.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${buildDirectory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
<destFileName>ns-core-1.0-SNAPSHOT.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
<finalName>nshub</finalName>
</build>
</project>

I guess that the
https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#outputFileNameMapping
outputFileNameMapping parameter allows you to customise that.

Related

Include xml files in maven project

I have a maven pom file to build from a structure like:
package1
--1.java
--2.java
--packageMetaInfo.xml
package2
--21.java
--22.java
--packageMetaInfo.xml
When I do a maven compile, the xml files don't come in the target.
maven-compiler-plugin 3.5.1 - Unless I exclude the xmls through <exclusions>, I get an error that "Fatal error compiling: All compilation units must be of SOURCE kind ->"
maven-compiler-plugin 2.0.1 - compiles but skips the xmls
Is there a way I can have the xmls included in my jar. The structure would be
x.jar
package1
--1.class
--2.class
--packageMetaInfo.xml
package2
--21.class
--22.class
--packageMetaInfo.xml
*I understand it may not be maven standard to have xml with source files but I am working on a specific product and need to maintain this structure in both input and output.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.incent</groupId>
<artifactId>Release-4.3.0.1.0.0001</artifactId>
<name>AAA Custom code</name>
<version>2.4.0.1</version>
<properties>
<ormb.cmccb.path>./Active_Repository/CMCCB</ormb.cmccb.path>
<ormb.customcode.path>${ormb.cmccb.path}/data</ormb.customcode.path>
<ormb.release.name>AAA-4.3.0.1.0.0001</ormb.release.name>
<ormb.target.path>./target</ormb.target.path>
<ormb.output.path>Release-${ormb.release.name}/Application/${ormb.release.name}/CMCCB</ormb.output.path>
<ormb.serverfile.output.relpath>./target/server</ormb.serverfile.output.relpath>
<build.number>SNAPSHOT</build.number>
</properties>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>icu4j</groupId>
<artifactId>icu4j</artifactId>
<version>49.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>wlfullclient</groupId>
<artifactId>wlfullclient</artifactId>
<version>10.3.4.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>${ormb.customcode.path}/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>copy-cm</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${ormb.customcode.path}/etc/lib</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${ormb.target.path}</directory>
<includes>
<include>cm.jar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-mwpackage</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${ormb.serverfile.output.relpath}</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${ormb.target.path}</directory>
<includes>
<include>Release-${ormb.release.name}.zip</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<encoding>ISO-8859-1</encoding>
<source>1.7</source>
<target>1.7</target>
<includes>
<include>**/cm/**</include>
</includes>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
<resources>
<resource>
<directory>${ormb.customcode.path}/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>create-cm</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>Active_Repository/assembly/executable.xml</descriptor>
</descriptors>
<finalName>cm</finalName>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<archive>
<manifestEntries>
<Specification-Title>${project.name}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Version>${build.number}</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
</execution>
<execution>
<id>create-distro</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>Active_Repository/assembly/dist.xml</descriptor>
</descriptors>
<finalName>custom-action-dist</finalName>
<appendAssemblyId>false</appendAssemblyId>
<finalName>Release-${ormb.release.name}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I faced the same issue, I wanted to keep fxml & java files in the same folder (I'm using scenebuilder who needs both fxml and java files at the same place).
Here is my solution in pom.xml:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
If you want the xml files to be included in your JAR file, then store them in the resources folder, instead of java.
Presumably they're in src/main/java. Try moving them to src/main/resources. Anything in src/main/resources will be packaged into the JAR file along with the .class files.

How to get artificactId of dependencies in Maven?

I have multi-module project. Dependencies are as follows:
<dependencies>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-places-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-devices-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-events-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-controls-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gwr</groupId>
<artifactId>gwr-logs-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
And, under src/main/resources of each module/dependency I have one ZIP file named as <module>-persistence.zip (e.g. C:\gwr-logs-module\src\main\resources\logs-persistence.zip). I want to copy this ZIP file of each module under some other directory let us say C:\users\user\project. I have defined maven-resources-plugin as below:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-presubscriptionfiles</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${user.home}/${dasmo.storage}/</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${basedir}/../gwr-logs-module/src/main/resources</directory>
<includes>
<include>*.zip</include>
</includes>
</resource>
<resource>
<directory>${basedir}/../gwr-devices-module/src/main/resources</directory>
<includes>
<include>*.zip</include>
</includes>
</resource>
<resource>
<directory>${basedir}/../gwr-controls-module/src/main/resources</directory>
<includes>
<include>*.zip</include>
</includes>
</resource>
<resource>
<directory>${basedir}/../gwr-events-module/src/main/resources</directory>
<includes>
<include>*.zip</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
As you see, under resource section I have to hard code each module name. How can I avoid this? Can't we use artifactId of each dependency? If yes, how to use it? Or is there any other way to do this job?
Thank you so much.
regards,
Yeshwant
You can reduce the <plugin> section in your multi-module POM to the following:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-presubscriptionfiles</id>
<!-- Note: this phase is more appropriate than 'validate' -->
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${user.home}/${dasmo.storage}</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<!-- or likewise
<directory>${project.projectDirectory}/src/main/resources</directory>
-->
<includes>
<include>*.zip</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
If you declare your multi-module project as <parent> in any of your sub-module POMs this will be inherited by them (see Introduction to the POM, Project Inheritance) and such performed at each sub-module build.
Note: basedir is deprecated in favor of project.basedir.
See Maven: The Complete Reference, Multi-module vs. Inheritance for further reading.

Can't bind maven-remote-resources-plugin to both bundle and process goals

I use the maven-remote-resources-plugin to get some resources from an artifact and also need to bundle some resources for use in another project.
I bind the maven-remote-resources-plugin to the bundle goal in the default section (not in a profile). And I bind the maven-remote-resources-plugin to the process goal in a profile.
My problem is that I don't get the shared resources when using the profile (I don't get the target\maven-shared-archive-resources folder).
If I remove the maven-remote-resources-plugin in the default section (the bundle binding) it works fine.
Any suggestions?
Below is 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<dependencies>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>bundle</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<resourcesDirectory>${basedir}/src/test/resources</resourcesDirectory>
<includes>
<include>**/*.sql</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>create-test-data</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<testResources>
<testResource>
<directory>${basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.build.directory}/maven-shared-archive-resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<configuration>
<resourceBundles>
<resourceBundle>com.mycompany.app:my-app-common:1.0-SNAPSHOT:test-jar</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
</configuration>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
The problem was that the property outputDirectory is defined for both the process and bundle goals and I redefined it in the bundle goal.

Why my Maven project automatic generate apidoc in target folder?

I didn't configurated maven-javadoc-plugin in my maven project pom.xml file,but when I run mvn install command,then generate apidoc?
configuration of pom.xml as follow :
<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>
<parent>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<build>
<finalName>webcnmobile</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- web.xml is not mandatory since JavaEE 5 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
the spring-security-oauth-parent pom.xml see here
The javadoc plugin is configured in the parent pom that you are referencing (spring-security-oauth-parent). This means that you inherit this configuration and it automatically gets applied to your project.
The configuration in the parent pom is as follows:
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This is then being applied to your project and hence you are getting javadoc generated.

Maven exclude language files from jasig CAS

I searched to disable all languages and set the default language to english. So that only english is available. But i think there is no possibility to do this in jasig CAS.
So i'm trying to remove all language files from the final .war file of my jasig CAS build.
Her is my pom.xml file:
<modelVersion>4.0.0</modelVersion>
<groupId>lu.ion.cas</groupId>
<artifactId>cas-ion</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cas-ion Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<version>${cas.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-ldap</artifactId>
<version>${cas.version}</version>
<type>jar</type>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<cas.version>3.5.1</cas.version>
</properties>
<repositories>
<repository>
<id>ja-sig</id>
<url>http://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>cas-ion</warName>
<excludes>
<exclude>**/messages_*.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
The part who is supposed to remove those files:
<excludes>
<exclude>**/messages_*.properties</exclude>
</excludes>
But those files are still there.
Use Maven War plugin Overlays described here: http://maven.apache.org/plugins/maven-war-plugin/overlays.html
In this case pom is like this:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<overlays>
<overlay>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<excludes>
<exclude>WEB-INF/classes/messages_*.properties</exclude>
</excludes>
</overlay>
</overlays>
</configuration>
If you want include a language, add another overlay like this:
<overlay>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-webapp</artifactId>
<includes>
<include>WEB-INF/classes/messages_en.properties</include>
</includes>
Use webResources element. http://maven.apache.org/plugins/maven-war-plugin/faq.html#webresourcesexclude
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<resource>
<directory>path_to_messages</directory>
<excludes>
<exclude>messages_*.properties</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>

Resources