Maven plugin inheritance from pluginManagement - maven

there
I have a parent pom.xml which defines the the default configuration for maven-ear-plugin
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact1</artifactId>
</jarModule>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact2</artifactId>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</pluginManagement>
In the child pom, I defined the maven-ear-plugin in the <plugins> section
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<executions>
<execution>
<id>default-ear</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<earSourceDirectory>EarContent</earSourceDirectory>
<defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact3</artifactId>
</jarModule>
</modules>
</configuration>
</execution>
</executions>
</plugin>
My intention is to include all 3 jar files artifact1, artifact2 and artifact3 in the ear. However, after the build, only artifact3 defined in child pom.xml is included. So it looks like by default the <modules> definition in the child pom.xml overwrites what's defined in the parent pom, instead of merge them together. In fact, if I remove the whole <modules> section in the child pom.xml, the artifact1 and artifact2 will be included after the build.
My question is whether there is a way to include all jar modules defined in parent pom and child pom. I have several ear projects and all of them need to include the same set of jar modules plus a few jar modules of their own. I am trying to move the common set of jar modules to the parent so that they are not repeated in each child pom.xml.
Update to clarify my projects relations:
parent pom.xml (define maven-ear-plugin in the pluginManagement section)
-- project1 pom.xml (multi-module project)
-- myJar-proj1
-- myWeb-proj1
-- myEar-proj1 (define maven-ear-plugin in the <build> section)
-- project2 pom.xml (multi-module project)
-- myJar-proj2
-- myWeb-proj2
-- myEar-proj2 (define maven-ear-plugin in the <build> section)

In order to achieve the merge behavior you should put the maven-ear-plugin under the plugins tag in the parent pom (rather than under pluginManagement).
Parent pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact1</artifactId>
</jarModule>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact2</artifactId>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
EDIT
After OP's clarified projects structure: the attribute combine.children="append" on the modules element, in the child projects:
<modules combine.children="append">
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact3</artifactId>
</jarModule>
</modules>
The parent should still define the plugin only in pluginManagement.

Related

create spring boot native image in a multi module project

i am trying to build an native image with graalvm and spring boot.
my project has several modules.when i try to build native image i got this error:
Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)
and when i define mainClass path(org.example.api.Application) in properties in parent pom file i got this error:
Error: Main entry point class 'org.example.api.Application' neither found on the classpath nor on the modulepath.
how can i define the module that contain main class for graalvm?
In your parent pom (the one where you declare all your modules) using the syntax
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
use the latest Spring Boot BOM as parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
then override the native profile
<profiles>
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
At this point in your modules (where you need native builds) you can set this build configuration:
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
At this point you will be able to compile the project(s) using the mvn -Pnative clean package

How do collect all dependencies for multi modules

Collect all the dependencies for multi modules
I have modules shown as below:
parent
|_module1
|_module2
|_distribution
parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<profiles>
<profile>
<id>distribution</id>
<modules>
<module>distribution</module>
</modules>
</profile>
</profiles>
</project>
Distribution pom.xml
<project>
<artifactId>distribution</artifactId>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<inherited>false</inherited>
<configuration>
<includeScope>compile</includeScope>
<outputDirectory>${project.build.directory}/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would like to collect all dependencies base on the profile.
profile1: all dependencies includes module1 and module2 jar itself.
profile2: all dependencies excludes module1 and module2 jar itself.
Profile2 mainly for development environment. We just need the dependency jars in the server, and we use Eclipse to deploy the module1 and module2 on the fly to the server.
I have tried to use maven-assembly to get all dependencies includes module1 and module2, but I don't how to have conditional in maven assembly to include / exclude module jar.
Thanks

New GWT maven plugin

I'm getting the error below when when I try to compile.
The goal is gwt:compile
I do set the moduleName as a variable.
The module name is com.example.app.App
Same thing command line ~/work/projects/gwt/app$ mvn gwt:compile "-DmoduleName=com.example.app.App"
Failed to execute goal
net.ltgt.gwt.maven:gwt-maven-plugin:1.0-rc-6:compile (default-cli) on
project mysandbox: The parameters 'moduleName' for goal
net.ltgt.gwt.maven:gwt-maven-plugin:1.0-rc-6:compile are missing or
invalid -> [Help 1]
On the other hand mvn package worked.
Here's my pom:
<?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.example</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<prerequisites>
<maven>${mavenVersion}</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenVersion>3.0</mavenVersion>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<launcherDir>${project.build.directory}/gwt/launcherDir</launcherDir>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Do not upgrade past 3.1 to avoid triggering https://issues.apache.org/jira/browse/MSOURCES-95 -->
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-6</version>
<extensions>true</extensions>
<configuration>
<sourceLevel>1.8</sourceLevel>
<failOnError>true</failOnError>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>app-client</module>
<module>app-shared</module>
<module>app-server</module>
</modules>
</project>
So, you have a multi-module Maven project. Invoking mvn gwt:compile will try to execute that "goal" on each of the 4 modules (root module and 3 submodules). Because your moduleName property (used to configure the moduleName property of the gwt:compile goal) likely only exists in the app-client submodule, gwt:compile fails when applied to the root module or the app-shared submodule.
If you want to build your project, run mvn package (and if you don't want to run tests, pass -DskipTests).
Technically, you could also run mvn gwt:compile, but directly inside the submodule. For that to work, you'd first have to mvn install the app-shared submodule; otherwise Maven won't be able to resolve the dependency (as you would no longer be executing the full "reactor build".
For many reasons (see http://blog.lexspoon.org/2012/12/recursive-maven-considered-harmful.html and http://blog.ltgt.net/maven-is-broken-by-design/ as starting points), I highly discourage this practice (mvn install is an anti-pattern; most of the time what you want is actually mvn verify); and I also discourage using any phase before package with multi-module builds (which boils down to only ever using mvn package, possibly with -DskipTests, and mvn verify)

Maven: How to Add JPA Project in EAR using Pom.xml

How can I add JPA Project in the EAR pom.xml file? I don't see the JPA Module in the Maven.
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<earSourceDirectory>EarContent</earSourceDirectory>
<generateApplicationXml>false</generateApplicationXml>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<jarModule>
<groupId>artifactGroupId</groupId>
<artifactId>artifactId</artifactId>
<bundleDir>APP-INF/lib</bundleDir>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>

How to remove version number from war file

I have a Dependency in child pom like this.
<dependencies>
<dependency>
<groupId>sample-groupID</groupId>
<artifactId>sfint</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
And I am using maven-ear-plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven.ear.plugin</artifactId>
<version>2.10</version>
<configuration>
<modules>
<webModule>
<groupId>sample-gropuID</groupId>
<artifactId>sfint</artifactId>
<version>1.0.0-SNAPSHOT</version>
<moduleID>WebModule_sfint</moduleID>
<bundleFileName>sfint.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
Now my problem is that I want my war file to be like this - sfint.war but I am getting it as - sfint.1.0.0-SNAPSHOT.war
Any help ??
Within the <build> tag specify the name that you need for the artifact as "finalName".
<build>
<finalName>${project.artifactId}</finalName>
</build>
You can configure maven-ear-plugin to omit the version information in the EAR file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
[...]
<fileNameMapping>no-version</fileNameMapping>
</configuration>
</plugin>
</plugins>
</build>
Use the outputFileNameMapping option for the maven-ear-plugin plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<outputFileNameMapping>#{artifactId}##{dashClassifier?}#.#{extension}#</outputFileNameMapping>
This will solve your problem:
<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>myEAR</groupId>
<artifactId>myEAR</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>ear</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<earSourceDirectory>EarContent</earSourceDirectory>
<generateApplicationXml>false</generateApplicationXml>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<fileNameMapping>no-version</fileNameMapping>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>myWAR</groupId>
<artifactId>myWAR</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
</dependencies>
</project>
your final EAR will be myEAR-0.0.1.SNAPSHOT.EAR that contains myWAR.war (without version number)
if you want to remove version number from EAR also than add myEar
If the war/ejb is part of an ear project, one should use bundleFileName to change the module names in the final ear artifact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>${version.ear.plugin}</version>
<configuration>
<modules>
<webModule>
<groupId>com.foo</groupId>
<artifactId>myapp-web</artifactId>
<contextRoot>/myapp</contextRoot>
<bundleFileName>myapp-web.war</bundleFileName>
</webModule>
<ejbModule>
<groupId>com.foo</groupId>
<artifactId>myapp-ejb</artifactId>
<bundleFileName>myapp-ejb.jar</bundleFileName>
</ejbModule>
</modules>
</configuration>
</plugin>

Resources