Maven versions plugin: reference a rule.xml from a maven dependency? - java

I am using the mvn versions:display-dependency-updates versions:display-plugin-updates goals to check for dependencies or plugins updates.
My maven project is a multi module one, which looks like this:
moduleA
|- moduleB1
| |- moduleC
|- moduleB2
|- build-config/rules.xml
Since there is some unwanted updates, like betas I don't want, I've made a filter (which works). I use it like that:
<profile>
<id>maven-version-plugin-1</id>
<activation>
<property>
<name>version.rules.uri</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<rulesUri>${version.rules.uri}</rulesUri>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I am forced to use a profile and a property version.rules.uri because it must refer to an existing file (by default it points to ./build-config/rules.xml, but it is also in my settings.xml with an absolute path).
I'd like to avoid that by:
publishing an independent build-config project
referencing this project using some uri: m2:myGroupId:myArtifactId:version:scope:jar/rules.xml
Now the question: is there an implementation of Maven Wagon Plugin (which is used by maven versions plugin) that allow for reading a repository entry such as a jar ?

This works for me:
<rulesUri>file:///${session.executionRootDirectory}/maven-version-rules.xml</rulesUri>
For the meaning of the variable ${session.executionRootDirectory}, see
Finding the root directory of a multi module maven reactor project.

Based upon the documentation for the plugin this is possible:
You can provide your ruleset xml file also within a jar, if you want to distribute your ruleset xml as Maven artifact. Therefore you have to declare the containing jar as direct dependency of the versions-maven-plugin and to use classpath as protocol.
I just tried it out and got it to work.
Create a new folder for the new version-rules artifact, as so:
version-rules
|- files
\- version-rules.xml
\- pom.xml
The pom.xml is pretty basic:
...
<artifactId>my-version-rules</artifactId>
<packaging>jar</packaging>
<build>
<defaultGoal>package</defaultGoal>
<resources>
<resource>
<directory>files</directory>
<filtering>false</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
...
run a mvn install to install this artifact.
Then, in the other pom, you configure the versions plugin as follows:
...
<build>
...
<pluginManagement>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<rulesUri>classpath:///version-rules.xml</rulesUri>
</configuration>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>my-version-rules</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
...
</plugins>
...
</pluginManagement>
...
</build>
...

Related

How to properly setup maven Spring Boot multi module project?

After some research try and fail, I am still not able to put my head around a clear way to do the following:
Project-All - "Ability to combine Module #1 & Module #2 and to run in dev (Test full solution)"
|
+ Module_1 - "Ability to run independently in dev (Different dev team)"
| - pom.xml
+ Module_2 - "Ability to run independentlyin dev (Different dev team)"
| - pom.xml
- pom.xml
I would like to build and run module_1 and module_2 separately, as well as assembled. After trying Assembly Plugin for Maven without significant success (Or over-complicated solution), I am now trying with Spring Boot Plugin for Maven which seems way simpler to use.
So would we have any recommendation on how to properly build such setup with Spring Boot Plugin for Maven?
Thanks
Here is one approach you can try :
In the main module POM you could use the maven release plugin like follows :
...
<packaging>pom</packaging>
<modules>
<module>module1..</module>
<module>module2..</module>
</modules>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<goals>install</goals>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
</build>
If you wish to build the submodules in such a way that they could run on its own :
your could use maven repckage goal in the module pom as follows:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
There is possibility providing the name of the main class in this configuration.
Checkout the official documentation

Maven Build - Class Files Size is reduced

I am trying to mavenize an existing project.
I was able to build the EAR file(since i have to deploy in Websphere), When I try to deploy, using admin console - Able to install successfully , But application is not working, After investigating, I found the class files size is very less compare to the reference EAR file(old existing EAR file)
Steps I followed to build the EAR file
M2E plugin installed
Configure to Maven
Add ALL the jar files from lib folder like below(I read in SO, this is not the recommended way, but to complete the project, I have to do this)
<dependency>
<groupId>JarFile</groupId>
<artifactId>JarFile</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/CRDBXMLExternal.jar</systemPath>
</dependency>
Added the relevant plugins (war, EAR)
Clean Build and Install.
ear file created. ear contains a war file, which has all the project related files including class,jsp etc.
I compared the folder structure with the existing EAR file and its contents , all look good. But only the size of class files(Not ALL but more than 80%) are varying. I use JD to decompile and see the code, Most of the code are not present, including imports.
If anyone has encountered similar issue , could you please tell me what am doing wrong here.
More Info
there are two project folders(both are maven) one will create WAR and another one EAR in EAR pom.xml
there is a dependency
<dependency>
<groupId>com.comp.abc</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
Then there is a plugin
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<version>5</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<generatedDescriptorLocation>C:\COMP\Dev\may\repos\0.0.1-SNAPSHOT</generatedDescriptorLocation>
</configuration>
</plugin>
</plugins>
</build>
Adding WAR file building(Removed most of the dependencies kept only one sample) 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>com.comp.abc</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>ABC</name>
<description>ABC</description>
<dependencies>
<!-- Local Repository -->
<dependency>
<groupId>com.ibm.ws.runtime</groupId>
<artifactId>com.ibm.ws.runtime</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/com.ibm.ws.runtime.jar</systemPath>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-releases</id>
<name>nexus</name>
<url>http://abc-nexus.ldn.xyz.com:9080/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- <warSourceDirectory>${project.basedir}\WebContent</warSourceDirectory> -->
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
The above does not tell Maven to package the EAR file with the lib directory dependency. It actually tells it to create local dependency on an existing JAR that is provided only at compile time. Thus, when you export the EAR, it does not include any of the JARs because it assumes that they are provided at runtime.
You should use the maven-ear-plugin which package an EAR file instead. You can find the full documentation here.
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.1</version>
</plugin>
The issue was,
web/WebContent/WEB-INF/classes is not getting updated.
but I could see the latest classes under web/target/classes path.
Now am checking why web/WebContent/WEB-INF/classes is not getting updated.
Just now got the Resolution from the below Link :-
ISSUE SOLVED by with the help of
https://coderanch.com/t/474423/ide/ecplise-doesn-create-classes-folder
Steps
Right click on your project -> build path -> Configure build path -> click on source tab -> click on browse (Default output folder).
After browsing click on WebContent -> Select WEB-INF -> Create new folder (called classes). it will open new window.
Give folder name as classes. Click on Advanced and give path of current classes folder means WEB-INF/classes.
After doing this, eclispe will rebuild your project and classes will be genenrated at WEB-INF/classes directory.

Is there a way to create a jar file in a maven project only with the enums or interfaces?

I have a project with a standard pom and jar packaging.
The standard -.jar is a desirable artifact of this project.
Additionally to the standard jar with all the classes of this project i need to create an additional jar only with the enums declared on it.
If i am abble to generate this jar in a maven standard with a different artifactId would be a plus.
This question here Create several jar files from single project using maven would be a good starting point but there is no answer
Any ideas?
Your best bet is to have a Maven multi-module build.
Typically, this consists of a single parent module plus multiple child modules.
The packaging of the parent module should be "pom" and it will only act as a container of the child modules.
The parent's pom.xml will look something like this:
<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>your groupID</groupId>
<artifactId>artifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Name</name>
<modules>
<module>submodule1</module>
<module>submodule2</module>
</modules>
</project>
Each module should be the name of a subdirectory. These subdirectories will contain their own pom.xml files.
Note that the groupId, artifactId, and version will be used later, so they should be something meaningful.
Your two child modules should look like normal jar packaging POM files with a few exceptions:
Each module should have a <parent> element points to the <groupId>, <artifactId>, and <version> of the parent module. While this section isn't strictly necessary, it does let you define common dependencies in the parent module.
The regular module will have the enum module as a dependency. You declare this just like any other dependency, but be aware that Maven compiles modules in order, so the enum module should be in the parent pom first.
I suggest you break your classes into two maven artifacts: one with the Enums and Interfaces as a dependency of the other which has your implementations. Since each has it's own POM you'll get two jars out of a build.
I end up using profiles to manage the solution solution. One managing the maven-jar-plugin plugin and other for the maven-source-plugin.
<profiles>
<profile>
<id>enum</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classifier>enum</classifier>
<includes>
<include>**/*Enum**</include>
<!-- or any other way to math my artifacts -->
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>enum-sources</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${mavenSourcePluginVersion}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
<configuration>
<classifier>enum-sources</classifier>
<includes>
<include>**/*Enum**</include>
<!-- or any other way to math my artifacts -->
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In this way i can use
mvn install -Penum
to generate the jars with only the enums
and
mvn install -Penum-sources
to generate the jars with the sources.
to include this dependencies in other maven projects all you need is to specify the profile with classifier as in
<dependency>
<groupId>${groupId}</groupId>
<artifactId>{artifactId}</artifactId>
<version>${version}</version>
<classifier>enum</classifier>
</dependency>
<dependency>
<groupId>${groupId}</groupId>
<artifactId>{artifactId}</artifactId>
<version>${version}</version>
<classifier>enum-sources</classifier>
</dependency>

How to package docs artifact within WAR

I've got the maven-enunciate-plugin integrated so that it generates documentation during the build and outputs it to the docs directory under the target directory. As I'm new to Maven I would like to know what would be the ideal way to configure my build so that it packages the docs directory into the WAR artifact of my build. Currently it is left outside of the WAR.
Thanks,
Mike
Please take a look at this and this
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<!--
Exclude JCL and LOG4J since all logging should go through SLF4J.
Note that we're excluding log4j-<version>.jar but keeping
log4j-over-slf4j-<version>.jar
-->
<packagingExcludes>
WEB-INF/lib/commons-logging-*.jar,
%regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
</packagingExcludes>
<packagingIncludes>
**/docs
</packagingIncludes>
</configuration>
</plugin>
</plugins>
</build>
...
</project>

Maven war plugin not able to exclude libs in exploded war format

I am using maven war plugin to exclude some common jar and put them in the classpath. I am able to generate war file properly which excludes specified libs and add them in the classpath but exploded war directory still contains excluded libararies. How can I generate exploded war file which use configuration of maven war plugin.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ekaplus.ctrm.mdm</groupId>
<artifactId>core-presentation</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>presentation layer core</name>
<dependencies>
<dependency>
<groupId>com.ekaplus.ctrm.mdm</groupId>
<artifactId>eka-core-mdm-presentation</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>exploded</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<packagingExcludes>
WEB-INF/lib/dto-common-1.0.jar,
WEB-INF/lib/eka-action-1.0.jar
</packagingExcludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
(...) How can I generate exploded war file which use configuration of maven war plugin.
The war:exploded goal does use the (global) configuration of the maven war plugin but it doesn't admit the packagingExcludes parameter of war:war. This can't work.
But why you are using packagingExcludes anyway? This parameter should be used to implement the very special skinny war use case, and I'm not sure it's your case. Why do you need it?
Depending on your exact needs, my suggestion would be to play with dependency scopes (provided in your case?) or profiles (to add dependencies) or a combination of both.

Categories

Resources