Maven pom.xml - build modules + self build - java

I have the next project structure:
app
module1
module2
...
moduleN
parent-pom
Project app contains only properties files and JasperReports's reports templates. Pom.xml of app:
<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>my-group</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0</version>
<relativePath>../parent-pom</relativePath>
</parent>
<artifactId>app</artifactId>
<version>${app-version}</version>
<packaging>pom</packaging>
<modules>
<module>../module1</module>
<module>../module2</module>
...
<module>../moduleN</module>
</modules>
I want to add jasperreports-maven-plugin for project app. I've tried next code (before <modules> section), but it doesn't work (self build wasn't called):
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jasperreports-maven-plugin</artifactId>
<configuration>
<sourceDirectory>${basedir}/config/templates</sourceDirectory>
<outputDirectory>${basedir}/config/templates</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>compile-reports</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasperreports.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Is it possible to add self build to pom with modules?

The app project packaging is set to pom - so it can't have any other artifacts. You will need to move the jasper reports to one of the other modules or a separate module.

Related

Publish binary file to maven artifactory along with JAR files using Jenkins

I am trying to publish a Java library to artifactory using Jenkins. Currently, it is only publishing .jar files to artifactory. I have a binary file (.bin) that I want to publish along with the .jar files. Does anyone know what I need to insert into my POM file to make this work?
For example, I want file structure in artifactory to look like this:
test-0.1.jar
test.bin
...
Here is my POM file:
<?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.github.test</groupId>
<artifactId>test</artifactId>
<version>0.1</version>
<name>Test Lib </name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- Avoid Java 8's strict doc generator-->
<profiles>
<profile>
<id>disable-java8-doclint</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<properties>
<additionalparam>-Xdoclint:none</additionalparam>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<source>8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The build helper maven plugin allows you to attach additional artifacts, see
https://www.mojohaus.org/build-helper-maven-plugin/usage.html
under "Attach additional artifacts to your project" or
https://www.mojohaus.org/build-helper-maven-plugin/attach-artifact-mojo.html

Integration in Maven module project

I am facing issues in organizing Maven multi-module projects. I want to use the files in one child project(B) from other child projects which are C, D and E. Below is complete scenario described.
I have Parent project A and its child projects are B(output jar), C(output pom) and D(output pom). Project C has its own four children(F, G, Example1 and Example2). And, project D has its children which is named as E(output pom). It looks like this:
Project A(parent)
|-- project B(child1; outputs jar which is my GUI)
|-- project C(child2; outputs pom)
|-- project F(child of project C, outputs war)
|-- project G(child of project C, outputs war)
|-- project Example1(child of project C, outputs war)
|-- project Example2(child of project C, outputs war)
|-- project D(child3; outputs pom)
|-- project E(child of project D; outputs pom)
Now, project B is my GUI of the project which internally uses the some of the files from project C, D, E, F. G. Example1 and 2. During programming I am able to execute the files from all projects in project B. But problems come when I release the final package of the project and use only jar of the project B because I am unable to execute files from the other projects which are C, D, E, F. G. Example1 and 2. Could anyone please help me how to use other projects? I am beginner in the Maven so I do not have idea how to integrate files between different child projects.
Try with following way - hope it should work
The main idea is to create FAT jar of project-b using assembly plugin (refer project-b pom.xml file content below)
Basically, I created project structure as per your description
and pom file entries are like below:
(refer image for project structure at the end)
**project-a** pom.xml file content:
<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.test</groupId>
<artifactId>project-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>project-b</module>
<module>project-c</module>
<module>project-d</module>
</modules>
<build>
<finalName>project-b</finalName>
<pluginManagement>
<plugins>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<!-- test plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- War plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.test.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
**project-b** pom.xml file content
<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.test</groupId>
<artifactId>project-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-b</artifactId>
<properties>
<jdk.version>1.7</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>project-c</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>project-d</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>project-e</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- test plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.test.MainApp</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
**project-c** pom.xml file content
<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.test</groupId>
<artifactId>project-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-c</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- War plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
**project-d** pom.xml file content
<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.test</groupId>
<artifactId>project-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-d</artifactId>
<packaging>pom</packaging>
<modules>
<module>project-e</module>
</modules>
</project>
**project-e** pom.xml file content
<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.test</groupId>
<artifactId>project-d</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-e</artifactId>
<build>
<plugins>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<!-- test plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Finally open git bash or cmd from folder project-a and run following maven command
mvn clean install -DskipTests
it will generate fat jar (project-b-jar-with-dependencies.jar, see in image) of project -b and output should be something like below-
[INFO] project-a ......................................... SUCCESS [ 1.084 s]
[INFO] project-c ......................................... SUCCESS [ 2.525 s]
[INFO] project-d ......................................... SUCCESS [ 0.019 s]
[INFO] project-e ......................................... SUCCESS [ 1.552 s]
[INFO] project-b ......................................... SUCCESS [ 2.873 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.369 s
[INFO] Finished at: 2018-05-23T23:51:25+05:30
[INFO] Final Memory: 22M/172M
[INFO] ------------------------------------------------------------------------
Project structure image:

Include source package and dependency only for a profile with Spring Boot maven plugin

In a Spring Boot based project of mine I want to create two different builds from the same project.
The decision on which build is generated should come from a maven profile.
I want to create one build (full) which includes a certain folder src/main/java/com/example/demo/full and a certain dependency, and a second build (default or light) build which does not include them.
Including the dependencies for build full works, but I don't know how to make sure the folder src/main/java/com/example/demo/full is only compiled for the full build.
Here 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>full</id>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
How can I manage to have the mentioned source-folder only compiled for profile full?
Add a second src folder like scr\foo and then add a profile in maven configure this src folder.
<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>
...
</build>
<profiles>
<profile>
<id>extraSource</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/foo/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Here the source folder is added using the Build Helper Plugin plugin for maven. As it is embedded in the build section of the specific profile, it is only active while executing maven with this profile (see the activation section)
there are problem with disabling one of your maven-source-plugin if this dependency is a part of parent which you cant not give ID to, ill recomend to use phase none with this code to one of your pom.xml files that will disable this.
I also recommend to use command: mvn -Prelease-profile help:effective-pom
to print if you have two of dependencies maven-source-plugin in your code, if yes, disable one of them with this code below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>none</phase>
</execution>
</executions>
</plugin>

Maven ear contains old version of war file

I'm creating an ear package using maven which will contain a war file of the module. When I run mvn clean install from the ear pom folder location, maven just copies the existing war file into the ear. What I want is maven to create a fresh war file and package it up into the ear. So I have to first run mvn clean install for the war and then for the ear.
Below is the pom.xml for the ear. Is there anything missing from the 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>
<artifactId>abc-module-ear</artifactId>
<packaging>ear</packaging>
<name>${project.artifactId}</name>
<properties>
<parent.basedir>${basedir}/../..</parent.basedir>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>abc-module</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<parent>
<groupId>com.nab.cmservices</groupId>
<artifactId>cmservices-base</artifactId>
<version>1.0.16-SNAPSHOT</version>
</parent>
<build>
<plugins>
<!-- Configuration for "abc-module.ear" -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<version>5</version>
<displayName>abc-module-ear1</displayName>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>abc-module</artifactId>
<uri>abc-module.war</uri>
<contextRoot>/abcservices-web</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>

Retrotranslator Maven Plugin does not recognize backport packages on another project

I'm trying to use Retrotranslator Maven Plugin to facilitate development on a pre-JDK1.4 environment. I have a maven module called xyz-core where there is the package br.com.company.xyz.core.backport that I want to use as a universal backport package.
The problem is: the plugin does not find the package. If I copy that package to this project, it works. But I need the backport package on a separate artifact because it will be used in many other projects.
Here are the 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>br.com.company.xyz</groupId>
<artifactId>xyz-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>xyz-test</artifactId>
<dependencies>
<dependency>
<groupId>br.com.company.xyz</groupId>
<artifactId>xyz-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>retrotranslator-maven-plugin</artifactId>
<version>1.0-alpha-4</version>
<configuration>
<classifier>ME</classifier>
<target>1.1</target>
<verbose>true</verbose>
<verify>true</verify>
<verifyClasspath>
<element>${basedir}/lib/rt.jar</element>
</verifyClasspath>
<backport>br.com.company.xyz.core.backport</backport>
</configuration>
<executions>
<execution>
<goals>
<goal>translate-project</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Categories