Exclude directive within parent POM - java

I'm trying to exclude a complete folder to be embedded within a JAR. I found the following directive which works like a charm :
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/unwantedJars/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
So, when running mvn clean install, no problem, I get a JAR without the unwanted folder.
However, I have several projects which needs to include this directive (together with other common configuration), so I'm using a parent POM project. Everything is working well, apart the above directive. As soon as I move this exclude part to the parent POM definition, it doesn't work anymore.
Strange thing is that if I compare the effective POM of the 2 configuration, they're strictly identical!
What's the difference between having this directive on the POM of the current project or in the parent POM?

You need to use pluginManagement
parent-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">
...
<build>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/unwantedJars/**</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
...
</build>
</project>
child-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">
...
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
...
</build>
</project>

Related

How to create a non-executable JAR file that includes all Maven dependencies

I have a Java project that doesn't have a main file but it has a lot of Maven dependencies.
How I can create a JAR-file that contains my source-code and required maven dependencies?
My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>NetworkCommunicationAPI</groupId>
<artifactId>NetworkCommunicationAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
</project>
You can use the maven-assembly-plugin just as you could use it for creating an executable JAR with dependencies.
The only thing you need to change is that you don't need to specify a main class:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
After that, you can create the JAR using mvn compile assembly:single.
This compiles your sources and creates a JAR containing the compiled sources and all dependencies in the compile-scope.
You can use the Maven shade plugin or Maven assembly plugin to create such a JAR.
In most cases, such JARs should be avoided. You create a dependency hell for everyone who uses your JAR because Maven cannot manage the included dependencies any more and therefore conflicts with other dependencies (not from your JAR) may become unmanagable.
On the other, such fat JARs are of little use because Maven or Gradle will find your transitive dependencies anyway, so there is no need to include them into the JAR.

maven-jacoco-plugin: Build fails after adding class excluding

I am using maven jacoco plugin for code coverage. I decided to exclude some classes from jacoco coverage report. I found here, how to do it.
So my updated pom file looks 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>payment-rest</artifactId>
<packaging>war</packaging>
<name>payment-rest</name>
<parent>
<artifactId>payment-ws</artifactId>
<groupId>com.example.foo</groupId>
<version>1.0.0-INTEGRATION-SNAPSHOT</version>
</parent>
<properties>
<jacoco.line.coverage>0.78</jacoco.line.coverage>
<jacoco.branch.coverage>1.00</jacoco.branch.coverage>
<servlet.version>3.0.1</servlet.version>
</properties>
<dependencies>
<!-- all dependecies here-->
</dependencies>
<build>
<finalName>payment-ws</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
**/com/example/foo/payment/configuration/**.*
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
So, when I am running mvn clean install command, I gets this error ():
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
If I remove exclusion part, project builds successfully.
Can someone advise me how can I solve this issue?
issue is occur because maven-surefire-plugin is missing into your project pom.xml. you need to add this plugin into pom.xml, after adding need to update and rebuild the project and run it.
you can find the maven plugin below:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
you can refer higher version from 2.19.1.

spring boot package does not exist error

I'm compiling my project with mvn clean package, and failed with package does not exist.
The detail command:
Get the jar file target/xxxx.jar by running mvn clean package in source project.
install this jar file by running mvn install:install-file -Dfile=lib/xxxx.jar -DgroupId=com.company -DartifactId=source-package-name -Dversion=1.0.0 -Dpackaging=jar
add code to target project, which will use some functions in source project
compile target project by running mvn clean package, and it just failed with package does not exist
here is the source project pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>source-package-name</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>source-package-name</name>
<description>xxxx</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx6144m</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
here is the target project pom.xml
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>target-package-name</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>target-package-name</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<smartv-common.version>0.3.5s</smartv-common.version>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- Exclude those since they are copied from the profile folder for
the build -->
<exclude>system.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<finalName>xxxxx</finalName>
<!-- Set a compiler level -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
http://stackoverflow.com/questions/12729513/how-to-overwrite-files-in-the-war-file-during-maven-build -->
<webResources>
<!-- Resources from the activated profile folder -->
<resource>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>system.properties</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.
To configure a classifier of exec in Maven, the following configuration can be used:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>

Why doesn't the maven ear plugin include files I put in src/main/resources?

I have a dead simple pom:
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sandbox</groupId>
<name>Sandbox</name>
<artifactId>Sandbox</artifactId>
<version>7.0</version>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
</project>
When the packaging is marked as "jar", it gets all the resources at src/main/resources. But if the packaging is marked as "ear", it doesn't get any of those resources in the packaging. The reason I want to do this is because I want a glassfish-resources.xml file to be in my ear's META-INF directory. This is a valid place to put the file.

Getting Maven exec:java plugin to use project module dependencies

I'm using Maven's exec:java to run jline for one of my projects (current POM attached below). The project used to be a single component, so all dependencies were in the same POM as the exec:java plugin definition. This worked great and all the dependencies were picked up and put on the classpath when I ran 'mvn exec:java'. However, I've now split up the project into a few modules and would like the dependencies from each module to be picked up when exec:java is run, but I can't figure out how to configure it. Advice would be greatly appreciated!
thanks,
Nick
<?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>
<name>Lensfield</name>
<groupId>org.lensfield</groupId>
<artifactId>lensfield-pom</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</executableDependency>
<mainClass>jline.ConsoleRunner</mainClass>
<arguments>
<argument>clojure.lang.Repl</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<modules>
<module>lensfield-share</module>
<module>lensfield-build</module>
<module>lensfield-webapp</module>
</modules>
</project>
You can specify a parent POM for the project and define the exec-plugin in the pluginManagement section of the parent. This means that the plugin configuration will be available to any child POM that declares a minimal plugin configuration. The following would be sufficient.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
When the child is processed it will inherit the configuration from the parent, and the exec-plugin will be executed with the current project's dependencies.

Categories