Maven Packing Type Based on Profile - maven

I am using a found example on stackoverflow that dynamically changes the packaging type based on profile
Changing packaging based on active profile in pom
This is a snippet of my 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.scene7.is.qa</groupId>
<artifactId>jorogumo</artifactId>
<packaging>${packaging.type}</packaging>
...
<profile>
<id>JorogumoReportingWebApp</id>
<properties>
<packaging.type>war</packaging.type>
<final.name>jorogumo</final.name>
</properties>
...
Based on the upvotes from the answer link above I am assuming this should work, but I receive an error when cleaning
$ mvn clean
[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.scene7.is.qa:jorogumo:0.0.1-SNAPSHOT (/Users/xyz/working/_workspaces/Scene7/java/jorogumo/pom.xml) has 1 error
[ERROR] Unknown packaging: ${packaging.type} # line 10, column 13
Am I missing something? Or is this the wrong approach?
My end goal is to share common java classes between the web app and the command line jar.

I ended up creating a default packing.type property with a value:
<modelVersion>4.0.0</modelVersion>
<groupId>com.scene7.is.qa</groupId>
<artifactId>jorogumo</artifactId>
<packaging>${packaging.type}</packaging>
...
<properties>
<packaging.type>war</packaging.type>
</properties>
<profiles>
<profile>
<id>JorogumoReportingWebApp</id>
<properties>
<packaging.type>war</packaging.type>
<final.name>jorogumo</final.name>
</properties>
...

Related

How to supress maven-javadoc-plugin run form parent pom.xml?

I have a pom.xml https://repo1.maven.org/maven2/com/miglayout/miglayout-swing/5.2/miglayout-swing-5.2.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-parent</artifactId>
<version>5.2</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>miglayout-swing</artifactId>
<packaging>jar</packaging>
<name>MiGLayout Swing</name>
<description>MiGLayout - Java Layout Manager for Swing</description>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>miglayout-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
with this sources https://repo1.maven.org/maven2/com/miglayout/miglayout-swing/5.2/
And I got the following error:
INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 52.475 s
[INFO] Finished at: 2021-07-13T14:12:16+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:3.0.0:jar (attach-javadocs) on project miglayout-swing: MavenReportException: Error while generating Javadoc:
[ERROR] Exit code: 1 - Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
[ERROR] javadoc: error - The code being documented uses modules but the packages defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module.
Please tell me how to completely TURN OFF maven-javadoc-plugin? I need to build this project without javadoc.
Thank you
You can set the property maven.javadoc.skip to true, i.e. in your <properties> section add something like
<maven.javadoc.skip>true</maven.javadoc.skip>

Maven parent POM download

I'm new to Maven, so I'm wondering why Maven will actually download the parent.
Here is my sample directory:
├── hazriq-module
│ ├── document-generator
│ | ├── src folder
│ | └── pom.xml (document-generator)
│ └── pom.xml (hazriq-module)
└── pom.xml (hazriq-parent)
My hazriq-parent .pom file:
<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.hazriq</groupId>
<artifactId>hazriq-parent</artifactId>
<version>${hazriq.verion}</version>
<packaging>pom</packaging>
<properties>
<hazriq.verion>1.0.0</hazriq.verion>
</properties>
<modules>
<module>hazriq-module</module>
</modules>
</project>
My hazriq-module .pom file:
<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>
<parent>
<groupId>com.hazriq</groupId>
<artifactId>hazriq-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>hazriq-module</artifactId>
<name>hazriq-module</name>
<packaging>pom</packaging>
<version>${hazriq.verion}</version>
<modules>
<module>document-generator</module>
</modules>
</project>
My document-generator .pom file:
<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>
<parent>
<groupId>com.hazriq</groupId>
<artifactId>hazriq-module</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.hazriq.hazriq-module</groupId>
<artifactId>document-generator</artifactId>
<dependencies>
...
</dependencies>
When I try to run mvn install:
$ mvn install
[INFO] Scanning for projects...
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR] The project com.hazriq:hazriq-module:${hazriq.verion} (C:\development\hzrqmvn\hazriq-module\pom.xml) has 1 error
[ERROR] Non-resolvable parent POM: Failure to find com.hazriq:hazriq-parent:pom:1.0.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted
until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM # line 3, column 10 -> [Help 2]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException
My questions are:
1. Why will Maven actually go and try to download the hazriq-parent? from the repo.maven.apache.org? (Refer to the output of my mvn clean install above.
2. How can I successfully build my project?
Why wouldn't it? If you build a module, all its parent modules have to be resolved recursively. Same goes for all dependencies.
This means those artefacts have to be either in the configured remote repository (Maven Central by default) or your local one. You can add it to your local repository by doing a mvn install.
The reason my one is failing is because of the placeholder in the <version>.
I changed my parent .pom to:
<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.hazriq</groupId>
<artifactId>hazriq-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>hazriq-module</module>
</modules>
</project>
My module .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>
<parent>
<groupId>com.hazriq</groupId>
<artifactId>hazriq-parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>hazriq-module</artifactId>
<name>hazriq-module</name>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>document-generator</module>
</modules>
</project>
My document-generator .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>
<parent>
<groupId>com.hazriq</groupId>
<artifactId>hazriq-module</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hazriq.hazriq-module</groupId>
<artifactId>document-generator</artifactId>
</project>
A good read on this issue: https://jeanchristophegay.com/maven-unique-version-multi-modules-build-en/

How to build the simplest multi module Maven project using Windows command line?

I have multi module project which is very simple.
Directory structure:
C:\acme-project\parent
C:\acme-project\alpha
C:\acme-project\beta
Logical structure:
parent
/ \
alpha <- beta
I cannot build the beta because it depends on the alpha. And Meven doesn't want to build the alpha during the beta's build process!
C:\acme-project\beta> mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building beta 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.company:alpha:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.263 s
[INFO] Finished at: 2017-03-28T15:00:34+03:00
[INFO] Final Memory: 16M/220M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project beta: Could not resolve dependencies for project com.company:beta:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: com.company:alpha:jar:1.0-SNAPSHOT: Could not find artifact com.company:alpha:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
My POMs :
C:\acme-project\parent\parent.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>../alpha</module>
<module>../beta</module>
</modules>
</project>
C:\acme-project\alpha\alpha.com :
<?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">
<parent>
<artifactId>parent</artifactId>
<groupId>com.company</groupId>
<version>1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alpha</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
C:\acme-project\beta\beta.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>
<parent>
<groupId>com.company</groupId>
<artifactId>parent</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>beta</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>alpha</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
I can build the acme-project running mvn package in C:...\parent. But in case of more complex project parent could have 100 sub-modules. And obviously I don't want to build 98 modules (others that beta doesn't depend on) just to make Maven happy.
If you want to build just the projects that beta depends on, go to the root aggregator (i.e., the project with all the <modules>; parent in your case) and then ask Maven to build only beta and its dependencies in the current reactor:
cd parent
mvn package --projects com.company:beta --also-make
FYI, this can be abbreviated:
mvn package -pl :beta -am
In your minimal example, the above command will still build everything (as beta depends on alpha and implicitly on the parent), but in larger reactors it will build just the minimal subset necessary to build beta.

How to build a multi-module maven project in jenkins

I have a projects structure as follows:
ProjectParent
- pom.xml
ProjectApp
-pom.xml
ProjectAPI
-pom.xml
ProjectModels
-pom.xml
ProjectServices
-pom.xml
Etc..
ProjectModels/ProjectsServices are dependencies within ProjectAPI/ProjectApp.
Should I create separate jobs within Jenkins to build each module separately?
I created a job for ProjectAPP but get the following error below (Have set goals and actions to "clean install":
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject-app 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://maven.springframework.org/snapshot/com/myproject/myproject-common-config/0.0.1-SNAPSHOT/myproject-common-config-0.0.1-SNAPSHOT.pom
[WARNING] The POM for com.myproject:myproject-common-config:jar:0.0.1-SNAPSHOT is missing,
no dependency information available
Downloading: http://maven.springframework.org/snapshot/com/myproject/myproject-entities/0.0.1-SNAPSHOT/myproject-entities-0.0.1-SNAPSHOT.pom
[WARNING] The POM for com.myproject:myproject-entities:jar:0.0.1-SNAPSHOT is missing, no
dependency information available
Downloading: http://maven.springframework.org/snapshot/com/myproject/myproject-services/0.0.1-SNAPSHOT/myproject-services-0.0.1-SNAPSHOT.pom
[WARNING] The POM for com.myproject:myproject-services:jar:0.0.1-SNAPSHOT is missing, no
dependency information available
Downloading: http://maven.springframework.org/snapshot/com/myproject/myproject-persistence/0.0.1-SNAPSHOT/myproject-persistence-0.0.1-SNAPSHOT.pom
[WARNING] The POM for com.myproject:myproject-persistence:jar:0.0.1-SNAPSHOT is missing, no
dependency information available
Downloading: http://maven.springframework.org/snapshot/com/myproject/myproject-common-config/0.0.1-SNAPSHOT/myproject-common-config-0.0.1-SNAPSHOT.jar
......
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project myproject-app: Could not resolve dependencies
for project com.myproject:myproject-app:war:0.0.1-SNAPSHOT: The
following artifacts could not be resolved:
com.myproject:myproject-common-config:jar:0.0.1-SNAPSHOT,
com.myproject:myproject-entities:jar:0.0.1-SNAPSHOT,
com.myproject:myproject-services:jar:0.0.1-SNAPSHOT,
com.myproject:myproject-persistence:jar:0.0.1-SNAPSHOT: Could not find
artifact com.myproject:myproject-common-config:jar:0.0.1-SNAPSHOT in
org.springframework.maven.snapshot
(http://maven.springframework.org/snapshot)
ProjectParent 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.myproject</groupId>
<artifactId>myproject-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>myproject-entities</module>
<module>myproject-services</module>
<module>myproject-persistence</module>
<module>myproject-app</module>
<module>myproject-merchant</module>
<module>myproject-common-config</module>
<module>myproject-api</module>
</modules>
<dependencyManagement>
<dependencies>
...
</dependencies>
</dependencyManagement>
<repositories>
...
</repositories>
<build>
...
</build>
<properties>
...
<myproject-entities-version>0.0.1-SNAPSHOT</myproject-entities-version>
<myproject-services-version>0.0.1-SNAPSHOT</myproject-services-version>
<myproject-persistence-version>0.0.1-SNAPSHOT</myproject-persistence-version>
</properties>
</project>
ProjectApp Pom
<?xml version="1.0"?>
<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>
<parent>
<groupId>com.myproject</groupId>
<artifactId>myproject-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>myproject-app</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>myproject-app</name>
<url>http://maven.apache.org</url>
<dependencies>
...
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myproject-common-config</artifactId>
<version>${myproject-common-config}</version>
</dependency>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myproject-entities</artifactId>
<version>${myproject-entities-version}</version>
</dependency>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myproject-services</artifactId>
<version>${myproject-services-version}</version>
</dependency>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>myproject-persistence</artifactId>
<version>${myproject-persistence-version}</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<myproject-common-config>0.0.1-SNAPSHOT</myproject-common-config>
<myproject-entities-version>0.0.1-SNAPSHOT</myproject-entities-version>
<myproject-services-version>0.0.1-SNAPSHOT</myproject-services-version>
<myproject-persistence-version>0.0.1-SNAPSHOT</myproject-persistence-version>
</properties>
</project>
Am I using the wrong goals? Do I need to chain a number of commands? i.e. build other modules first?
I'm using Maven 3.
NOTE: I changed the target to "clean install" against the ParentProject Pom and everything builds correctly.
Thanks
The problem is that you can't create a project solely for the ProjectApp module because it depends on the other modules below the ProjectApp parent. If you don't deploy those modules to your maven repository, maven is not able to find them in the repository nor in the build reactor.
Instead you should create the job for the parent. This will build the necessary modules.
You may also work with the option also-make-dependants when you have a job for ProjectApp, but I haven't any experience with this.
Had the same problem, and SpaceTrucker answer helped me.
Hence I just run something like:
.../ProjectParent$ mvn -am -pl ProjectApp test

How to override maven property in command line?

I have the following plain pom running by Maven 3.0.4.
<?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>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
</project>
I am trying to override default settings in command line like this:
mvn -Dproject.build.finalName=build clean package
But this this is ignored, and I get test-1.0.jar. I've tried to change another properties, like outputDirectory, directory, artifactId, but also failed.
What is the proper way to do this thing?
See Introduction to the POM
finalName is created as:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
One of the solutions is to add own property:
<properties>
<finalName>${project.artifactId}-${project.version}</finalName>
</properties>
<build>
<finalName>${finalName}</finalName>
</build>
And now try:
mvn -DfinalName=build clean package

Resources