maven build ear - how I can build just a .ear file and not the exploded ear dir? - maven

I just need a myEar.ear file as output of my maven build.
I don't want the dir myEar (the exploded ear version).
To be clear, this is my build output:
20/02/2017 11:37 <DIR> myEar
20/02/2017 11:37 7.985.535 myEar.ear
I just want the myEar.ear.
Thanks.
Here my pom.xml of the ear:
<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>
<name>myEar</name>
<description>myEar</description>
<groupId>com.foo</groupId>
<artifactId>myEar</artifactId>
<version>1.0</version>
<packaging>ear</packaging>
<parent>
</parent>
<properties>
</properties>
<dependencies>
(...)
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<generatedDescriptorLocation>${basedir}/src/main/application/META-INF</generatedDescriptorLocation>
<modules>
<webModule>
<groupId>com.foo</groupId>
<artifactId>myWeb</artifactId>
<contextRoot>/myWeb</contextRoot>
</webModule>
<webModule>
<groupId>com.foo</groupId>
<artifactId>myWS</artifactId>
<contextRoot>/</contextRoot>
</webModule>
</modules>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</plugin>
</plugins>
<directory>${basedir}/../target/diraliases/EARHOMEDIR1036</directory>
<finalName>myEar</finalName>
</build>
</project>

I fixed with th maven-antrun-plugin... an ant call inside maven... very dirty... someone has a better idea?
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>deleteDir</id>
<phase>package</phase>
<configuration>
<tasks>
<delete dir="${basedir}/../target/diraliases/EARHOMEDIR1036/myEar"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

Related

Defining plugin goal at phase

I am trying to define a plugin goal so that it can automatically be executed at a particular phase; say for example the test phase. But I am unable to achieve that. For example I coded the following pom.xml. Notice in my XML <execution> element, I have a <phase> sub-element which is "test". I can execute my goal using the command "mvn antrun:run#ant-echo"; but "mvn test" does not execute that goal. I am using Maven version 3.8.3 and my JDK version is 16.
<?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>rjava.mvn</groupId>
<artifactId>app2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>app2</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.var1>value1</project.var1>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!--
The command to maven execute this ant target is
mvn antrun:run#ant-echo
-->
<id>ant-echo</id>
<phase>test</phase>
<configuration>
<target name="app2">
<echo level="info">This is how you echo information into the console.</echo>
<echo level="info">Echo levels defined in Ant are the following </echo>
<echo level="info">error, warning, info, verbose, debug</echo>
<echo level="info"></echo>
<echo level="info">The following is the way to echo property values.</echo>
<echo level="info">${project.var1}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
In order to have your plugin executed during the configured lifecycle phase, you have to specify the plugin goal binding and configuration within the <plugin> section:
<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>rjava.mvn</groupId>
<artifactId>app2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>app2</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.var1>value1</project.var1>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<!--
The command to maven execute this ant target is
mvn antrun:run#ant-echo
-->
<id>ant-echo</id>
<phase>test</phase>
<configuration>
<target name="app2">
<echo level="info">This is how you echo information into the console.</echo>
<echo level="info">Echo levels defined in Ant are the following </echo>
<echo level="info">error, warning, info, verbose, debug</echo>
<echo level="info"></echo>
<echo level="info">The following is the way to echo property values.</echo>
<echo level="info">${project.var1}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Maven project how run from cmd line passing argument

I want to run my mavn project from cmd line passing the argument "https://s3.amazonaws.com/dept-dev-swrve/full_stack_programming_test/test_data.csv.gz"
This is the 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.programming.test</groupId>
<artifactId>csv_parser</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>executable</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.21</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The code compiles okay but now I want to run from command line passing the one argument. How can I do this ?
You can set -D argument with the goals of your plugin.
E.g.
mvn -DskipTests=true, where skipTests is a goal and =True is it's value.

Maven - attach artifact with Maven descriptor files

For performance reasons I need to create a new EAR artifact beside a WAR archive without using multiple modules projekt. The EAR will contain the WAR (in the /web sub-directory) and the static application.xml file.
All I need to generate in the non-static way is Java classes and Maven descriptor files (META-INF/maven/${groupId}/${artifactId}/pom.xml and META-INF/maven/${groupId}/${artifactId}/pom.properties).
With classes there is no big deal, but how to generate and attach the Maven descriptor files to the new EAR?
This works well, but the Maven descriptor files are not generated inside the EAR archive.
...
<packaging>war</packaging>
...
<properties>
<compile.dir>${project.build.directory}/${project.build.finalName}</compile.dir>
<ear.zip>${project.build.directory}/my-archive.ear</ear.zip>
</properties>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>create-EAR-archive</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- copy webapp files -->
<copy todir="${compile.dir}/web">
<fileset dir="src/main/webapp" />
</copy>
<!-- copy classes -->
<copy todir="${compile.dir}/web/WEB-INF/classes">
<fileset dir="${project.build.directory}/classes" />
</copy>
<!-- copy application.xml -->
<copy todir="${compile.dir}">
<fileset dir="src/main/resources" includes="META-INF/application.xml" />
</copy>
<!-- copy some big files -->
...
<!-- zip EAR -->
<zip destfile="${ear.zip}" basedir="${compile.dir}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>attach-EAR-artifact</id>
<goals>
<goal>attach-artifact</goal>
</goals>
<phase>package</phase>
<configuration>
<artifacts>
<artifact>
<file>${ear.zip}</file>
<type>ear</type>
<classifier>exploded</classifier>
</artifact>
</artifacts>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The easiest thing is to have a multi module project.
A parent like :
<?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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<myproject.version>1.0-SNAPSHOT</myproject.version>
</properties>
<name>ear-example</name>
<modules>
<module>example-ear</module>
<module>example-war</module>
</modules>
</project>
A war as you have, and an ear project:
<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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>example-jar</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then just add any files you need to the src/main/application directory in the ear project and they will appear in the top level of your ear file.
See http://maven.apache.org/plugins/maven-ear-plugin/usage.html
The solution is to move the war package phase into another phase before the prepare-package phase, then there is a complete WAR archive containing the maven descriptor files. So it means, all the data are ready when the EAR archive is prepared.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
The archive unzip and use in the prepare-package phase as before:
<!-- unzip the war -->
<unzip src="${project.build.directory}/${project.build.finalName}.war" dest="${project.build.directory}/war-exploded" />
<!-- move the war content -->
<move todir="${compile.dir}/web">
<fileset dir="${project.build.directory}/war-exploded" />
</move>

How to share a filtered resource at generate-sources phase in a multi module project?

I have a parent project with 3 child projects:
parent
project-1
/src/main/resources/config.xml
project-2
/src/main/resources/config.xml
project-3
/src/main/resources/config.xml
The configuration config.xml is used during the generate-sources phase. For the three projects, the config.xml is exactly the same. However, the usage of this config.xml is different for each project.
In project-X, I am referring to config.xml as following:
<build>
<plugins>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>src/main/resources/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
What is the best way to share this common config.xml between all 3 projects?
You can use the build-helper-maven-plugin here.
PROJECT STRUCTURE
shared-resources-project
+-src
+-main
+-resources
`config.xml
+-project-A
`pom.xml
+-project-B
`pom.xml
+-project-C
`pom.xml
`pom.xml
shared-resources-project/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">
<modelVersion>4.0.0</modelVersion>
<groupId>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>project-A</module>
<module>project-B</module>
<module>project-C</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.parent.basedir}/src/main/resources</directory>
<includes>
<include>config.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>some-group</groupId>
<artifactId>some-artifact</artifactId>
<executions>
<execution>
<id>some-plugin-job</id>
<phase>generate-sources</phase>
<goals>
<goal>some-goal</goal>
</goals>
<configuration>
<input>${project.build.outputDirectory}/config.xml</input>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
shared-resources-project/src/main/resources/config.xml
<config>
<parameter>${custom-value}</parameter>
</config>
project-X/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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my</groupId>
<artifactId>shared-resources-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-X</artifactId>
<properties>
<custom-value>Project-X Value</custom-value>
</properties>
</project>
Now, let's build the project:
D:\workspaces> cd shared-resources-project
D:\workspaces\java\shared-resources-project> mvn clean install
Some notes:
The build-helper-maven-plugin will add the common config.xml file as a resource to Project-X.
Then the Maven resources plugin (MRP) will copy config.xml to the project output directory (target directory by default). During the copy, MRP will also replace ${custom-value} with the specific value provided by Project-X.
The final config.xml will be available to another plugin as long as the other plugin is bound to the generate-source phase AND its declaration appears AFTER the build-helper-maven-plugin declaration. Maven (3.0.4+ at least) calls the plugins in their order of apparition in the pom.xml.

maven: how to export packed sources with jar in "package" stage

I have a maven export script (that wasn't been written by me) and I would like to add sources coping as well.
the build script generates 2 outputs using the "maven-source-plugin":
.jar and -sources.jar and they are both exist in the same output folder one next to another.
so far only the jar is being copied, I want the script to place the -sources.jar file next to its jar file
the build 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>
<groupId>org.mytest</groupId>
<artifactId>my-parent</artifactId>
<version>6.0.00-SNAPSHOT</version>
<packaging>pom</packaging>
<name>my-parent</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<assembly.format>dir</assembly.format>
<my.repository.rootUrl>http://maven.my.com</my.repository.rootUrl>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
.
.
.
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<target>1.6</target>
<source>1.6</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
.
.
.
</modules>
<repositories>
.
.
.
</repositories>
<distributionManagement>
.
.
.
</distributionManagement>
<pluginRepositories>
.
.
.
</pluginRepositories>
<organization>
<name>My</name>
</organization>
<profiles>
<profile>
<id>dist</id>
<modules>
<module>../my-assembly/my-runner</module>
</modules>
</profile>
</profiles>
</project>
the export 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>org.mytest</groupId>
<artifactId>my-parent</artifactId>
<version>6.0.00-SNAPSHOT</version>
<relativePath>../../my-parent</relativePath>
</parent>
<artifactId>my-runner</artifactId>
<dependencies>
.
.
.
</dependencies>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>single</goal>
</goals>
<id>create-runner</id>
<phase>package</phase>
<configuration>
<finalName>runner</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/runner.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
the runner file (referenced by the export pom):
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
<format>tar.gz</format>
<format>zip</format>
<format>dir</format>
</formats>
<id>runner</id>
<dependencySets>
.
.
.
<dependencySet>
<outputDirectory>lib</outputDirectory>
<outputFileNameMapping>${artifact.artifactId}.${artifact.extension} </outputFileNameMapping>
<includes>
<include>org.mytest:*:jar</include>
<include>org.mytest.systemobjects:*:jar</include>
</includes>
<excludes>
<exclude>*:my-services-so:*</exclude>
<exclude>*:my-services-tests:*</exclude>
<exclude>*:my-runner:*</exclude>
</excludes>
</dependencySet>
</dependencySets>
<fileSets>
.
.
.
</fileSets>
</assembly>
thank you in advance!
From looking at MNG-1994 it seems that in each phase a child's plugins will execute before the parent's.
Your parent POM specifies the source plugin (which generates the sources JAR) in the package phase (this is the source plugin's default phase if not explicitly specified in the execution), but the assembly plugin, which generates the tar.gz, zip and uncompressed finished versions of your project, is run before the source plugin because the assembly plugin is declared in the child and the source plugin is declared in the parent.
A solution can be to change the phase the source plugin runs in to ensure it runs before the assembly plugin, something right before the package phase such as prepare-package will work. (full list of lifecycle phases is here)
In the parent, explicitly setting the phase to prepare-package will make it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
This should force the source plugin to execute before the assembly plugin and therefore the assembly plugin should pick up both attached JARs now.
Add another include element with the classifier sources:
<include>org.mytest:*:jar:sources</include>
See the documentation: http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet

Resources