Maven ear contains old version of war file - java

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>

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

Attach the snapshot version name with the JAR file

I run a Hadoop app with the following pom.xml file. The file contains all the info for creating the JAR file and required dependencies. The file is provided below:
<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.hadoop.wordcount</groupId>
<artifactId>wordcount</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wordcount</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.test.hadoop.wordcount.WordCount</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
</plugin>
</plugins>
</build>
</project>
When I run the app, it does create the JAR file wordcount.jar in the location,
However, I would like to attach the snapshot version with the JAR name ie. wordcount-0.0.1-SNAPSHOT.jar.
How do I achieve that? I don't have a lot of understanding of the snapshot versioning. I provided the Updated pom.xml file now.
UPDATE
I added the shade plugin but this doesn't solve the issue. Also, there is no JAR in the target directory.
Intellij w/ Maven generates the versioned JAR under target folder
Note: Since you're not using the Shade plugin, then your JAR shouldn't contain the dependencies that you may need

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>

Maven could not find artifact that already exists in repository

I run "mvn install" in command line and get stuck by below error.
Caused by: org.apache.maven.project.DependencyResolutionException: Could not resolve dependencies for project com.detux.vios:common-entity:jar:2_0_0:
Could not find artifact org.hibernate:hibernate-entitymanager:jar:3.6.2.FINAL in central (http://repo.maven.apache.org/maven2)
I check in m2\repository\org\hibernate\hibernate-entitymanager\3.6.2.FINAL\, here is its contents
I've also tried to add new remote repository to pom.xml but it still doesn't work
<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>
<artifactId>common</artifactId>
<groupId>com.detux.vios</groupId>
<version>2_0_0</version>
</parent>
<groupId>com.detux.vios</groupId>
<artifactId>common-entity</artifactId>
<version>${common.version}</version>
<name>Vios Common entity</name>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.detux.vios</groupId>
<artifactId>common-util</artifactId>
<version>${common.version}</version>
</dependency>
</dependencies>
<properties>
</properties>
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
</project>
Below is pom.xml of parent project
<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.detux.vios</groupId>
<artifactId>common</artifactId>
<version>2_0_0</version>
<packaging>pom</packaging>
<name>Vios Common</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>jar-test-classes</id>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<commons-jxpath.version>1.3</commons-jxpath.version>
<common.version>2_0_0</common.version>
<spring.version>3.0.5.RELEASE</spring.version>
<spring.integration.version>2.0.4.RELEASE</spring.integration.version>
<hibernate.version>3.6.2.FINAL</hibernate.version>
<maven-jaxb2-plugin.version>0.7.4</maven-jaxb2-plugin.version>
<commonsbeanutils.version>1.8.3</commonsbeanutils.version>
<commons-lang.version>2.6</commons-lang.version>
</properties>
<modules>
<module>common-model</module>
<module>common-entity</module>
<module>common-storage</module>
<module>common-util</module>
<module>common-service</module>
<module>common-service-business</module>
<module>common-service-resource</module>
<module>common-catalog</module>
<module>common-xml-jaxb2</module>
</modules>
</project>
I am not sure how this could have happened, but the central has this artifact but with Final in its folder path and not FINAL.
Can you change your pom.xml to use Final and delete the existing local repository folder m2\repository\org\hibernate\hibernate-entitymanager\3.6.2.FINAL so that it can download again?

Maven pom.xml - build modules + self build

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.

Categories