How to skip the maven antrun copy/shade:shade goal dynamically? - java

I am using maven for configuration of an application consisting of multiple small services. Most of the services developed in java share the same maven configuration, as in the same build lifecycle, some shared resources (like spring AMQP).
So I have organized the shared resources in a SuperPom.
While the shade plugin doesn't really seem to disturb the install process, the antrun plugin of course won't find any of the files it should copy, due to there not being created any jar files by the shade plugin.
As I'd like the configuration of the shade/antrun plugin to be abstracted in the SuperPom, I need to skip the shade/copy goal.
I have tried mvn clean install -Dmaven.shade.skip=true, mvn clean install -Dmaven.copy.skip=true, mvn clean install -Dmaven.shade.shade.skip=true
Here is a small sample for you to play with:
<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>Test</groupId>
<artifactId>SuperTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<log4j.version>1.2.17</log4j.version>
<destination>pleasedeleteme</destination>
<mainpackage>com.uk.cc.it.info.gov.test.xxx</mainpackage>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainpackage}.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${destination}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
</project>

Did you try setting the phase of maven-shade-plugin to none in the super-pom and then overriding this in the client poms?
So in the parent pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>shade</id>
<phase>none</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!-- ... -->
</configuration>
</execution>
</executions>
</plugin>
And in the child poms that need it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<!-- no need to specify version -->
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<!-- no need to specify configuration -->
</execution>
</executions>
</plugin>

The maven-shade-plugin doesn't have a parameter to skip. Often the shade-plugin isn't there just for fun, so you might wonder if you really want to skip this. If you think it is still valid, you have to create a profile with activation like this:
<activation>
<property>
<name>skipShade</name>
<value>!true</value>
</property>
</activation>
This way it is activated by default, unless you add -DskipShade or -DskipShade=true.

Maven 3.6.1 gives you a new approach.
In the superPom you can define a profile for your shading configuration:
<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>Test</groupId>
<artifactId>SuperTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<log4j.version>1.2.17</log4j.version>
<destination>pleasedeleteme</destination>
<mainpackage>com.uk.cc.it.info.gov.test.xxx</mainpackage>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${destination}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>shade</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${mainpackage}.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In the user's settings.xml under .m2 you can add a profile of the same id to enable the shade profile configuration of your superPom. This gives you the option to simple toggling the shading from inside your IDE like Intellij IDEA (only tested in Intellij).
<settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<!-- toggle shading from inside Intellij IDEA -->
<profiles>
<profile>
<id>shade</id>
</profile>
</profiles>
<!-- Shade Profile has to be activeProfile to be
able to explicitly disable shading -->
<activeProfiles>
<activeProfile>shade</activeProfile>
</activeProfiles>
</settings>
In the child project you can add a .mvn/maven.config file to your child project template to predefine shading for the project by default. (Requires a CVS template that is used to predefine a company standard.)
The approach using a maven.config is useful if some of your team members do not have the profile in their settings.xml file and you have to take care that shading will be done most of the time.
.mvn/maven.config:
-Pshading
The profile can also be activated by default using jenkinsfile for Jenkins by passing -Pshade. It will overwrite the maven.config setting. To disable use -P!shade
Please note if you are using maven.config file in Intellij (2020.2.2): The .mvn/maven.config file must exists in the subdirectory of the root aggregator pom folder. Building a subproject form the IDE does not respect a .mvn/maven.config file on the subproject level at the moment. Running a mvn command from the command line in the subproject folder will repespect both, the child project .mvn/maven.config and the parent .mvn/maven.config.

Disabling the maven shade plugin worked for me. The build was stock trying to produce the dependency reduced pom file before I disabled the Maven shade plugin.

The skip option was introduced in version 3.3.0 of the shade plugin, so now skipping can be done dynamically using, for example, properties:
<properties>
....
<skipShaded>true</skipShaded>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<skip>${skipShaded}</skip>
...
</configuration>
</execution>
</executions>
</plugin>
In the above the default is to skip, and this can be overridden with passing -DskipShaded=false to mvn.

Related

Using git-commit-id-plugin in maven works in package-phase but not in install-phase

I'm using the git-commit-id-plugin (see https://github.com/ktoso/maven-git-commit-id-plugin). It packages correctly when I'm setting up an annotated tag like e.g. v1.0.0, meaning the target-directory has a jar file named deploy-test-Test-v1.0.0.jar.
The problem is, that the maven install phase creates the following files in my local .m2-directory:
Test-${git.closest.tag.name}
|- deploy-test-Test-${git.closest.tag.name}.jar
|- deploy-test-Test-${git.closest.tag.name}.pom
|- _remote.repositories
I've tested this with the example pom.xml.
What can I do to get the same name (deploy-test-Test-v1.0.0.jar)?
<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.mytest</groupId>
<artifactId>deploy-test</artifactId>
<packaging>jar</packaging>
<version>Test-${git.closest.tag.name}</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>Test-${git.closest.tag.name}</revision>
</properties>
<dependencies/>
<build>
<plugins>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
<execution>
<id>validate-the-git-infos</id>
<goals>
<goal>validateRevision</goal>
</goals>
<!-- *NOTE*: The default phase of validateRevision is verify, but in case you want to change it, you can do so by adding the phase here -->
<phase>package</phase>
</execution>
</executions>
<configuration>
<!-- If you'd like to tell the plugin where your .git directory is, use this setting, otherwise we'll perform a search trying to figure out the right directory. It's better to add it explicitly IMHO. -->
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<prefix>git</prefix>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<dateFormatTimeZone>${user.timezone}</dateFormatTimeZone>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<format>properties</format>
<skipPoms>true</skipPoms>
<injectAllReactorProjects>false</injectAllReactorProjects>
<failOnNoGitDirectory>true</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>true</failOnUnableToExtractRepoInfo>
<skip>false</skip>
<runOnlyOnce>false</runOnlyOnce>
<useNativeGit>false</useNativeGit>
<abbrevLength>7</abbrevLength>
<commitIdGenerationMode>flat</commitIdGenerationMode>
<gitDescribe>
<skip>false</skip>
<always>false</always>
<abbrev>7</abbrev>
<dirty>-dirty</dirty>
<match>*</match>
<tags>false</tags>
<forceLongFormat>false</forceLongFormat>
</gitDescribe>
<validationProperties>
<validationProperty>
<name>validating project version</name>
<value>${project.version}</value>
<shouldMatchTo>
<![CDATA[^.*(?<!-SNAPSHOT)$]]>
</shouldMatchTo>
</validationProperty>
</validationProperties>
<validationShouldFailIfNoMatch>true</validationShouldFailIfNoMatch>
<evaluateOnCommit>HEAD</evaluateOnCommit>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
To incorporate git-commit-id plugin into the version number for the entire maven build cycle (till deploy)
<properties>
<java.version>1.8</java.version>
<snapshot.string>-SNAPSHOT</snapshot.string>
<!-- Snapshot Version Number -->
<!-- <version.number>${git.commit.time}.${git.commit.id.abbrev}${s`enter code here`napshot.string}</version.number> -->
<!-- Release Version Number -->
<version.number>${git.commit.time}.${git.commit.id.abbrev}</version.number>
<release.repo.key>libs-release-local</release.repo.key>
<snapshot.repo.key>libs-snapshot-local</snapshot.repo.key>
<artifactory.url>http://xxx.xxx.x.xxx:yyyy/artifactory</artifactory.url>
<release.repository.url>${artifactory.url}/${release.repo.key}</release.repository.url>
<snapshot.repository.url>${artifactory.url}/${snapshot.repo.key}</snapshot.repository.url>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dateFormat>yyyyMMdd.HHmmss</dateFormat>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<generateGitPropertiesFile>false</generateGitPropertiesFile>
<injectAllReactorProjects>true</injectAllReactorProjects>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>change-version</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<scripts>
<script>
< ![CDATA[
import org.apache.maven.artifact.versioning.VersionRange; git_revision = '${version.number}'
if (!project.properties['revision'] ? .trim()) {
println 'Change `version` to ' + git_revision
System.properties['revision'] = git_revision
project.properties['revision'] = git_revision
project.properties['project.version'] = git_revision
project.properties['git.build.version'] = git_revision
project.version = git_revision
project.artifact.version = git_revision
project.artifact.versionRange = VersionRange.createFromVersion(git_revision)
}
]] >
</script>
</scripts>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>release</id>
<name>local-releases</name>
<url>${release.repository.url}</url>
</repository>
<snapshotRepository>
<uniqueVersion>true</uniqueVersion>
<id>snapshots</id>
<name>local-snapshots</name>
<url>${snapshot.repository.url}</url>
</snapshotRepository>
</distributionManagement>
Also refer to this to work around the "plugin execution not covered by lifecycle" error in Eclipse/SpringToolSuite
see: How to solve "Plugin execution not covered by lifecycle configuration" for Spring Data Maven Builds
I've found a solution that worked fine for me. Just added the gmaven-plugin like in the pom-snippet below and the versions will be adapted to the last git-tag.
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>project.artifact.version='${git.closest.tag.name}';</source>
</configuration>
</execution>
</executions>
I've also used groovy-maven-plugin to update project.properties. Without this pom.xml was generated with variables that have not been parsed.
project properties
<properties>
<gitClosestTagName>${git.closest.tag.name}</gitClosestTagName>
<gitClosestTagCommitCount>${git.closest.tag.commit.count}</gitClosestTagCommitCount>
</properties>
build final name atteribute
<finalName>${project.artifactId}-${gitClosestTagName}.${gitClosestTagCommitCount}</finalName>
plugin definition
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>update-finalname</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.build.finalName="${project.artifactId}-${gitClosestTagName}.${gitClosestTagCommitCount}";
project.properties['gitClosestTagName']=${git.closest.tag.name};
project.properties['gitClosestTagCommitCount']=${git.closest.tag.commit.count};
println("project.build.finalName=${project.build.finalName}");
</source>
</configuration>
</execution>
</executions>

Settting up line separator in Maven

We are working on a multi module Maven project where in one sub project we have some resources file (free marker template files). When we create EAR out of the project depending on Operating System Maven is updating line separator in template files.
If we run mvn install on windows it keeps line separator as and when we run it on linux it changes line separator to .
Template are created on Windows machine and have default separator as , we don't want Maven to changes to even if we build project on windows / linux. As code is always deployed on Windows machine and fails when encounters linux line separator.
Is there any way to tell Maven not to mess up with line separators?
Project POM looks like
<project>
<parent>
<groupId>com.xyz</groupId>
<artifactId>Foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>FooBatch</artifactId>
<packaging>jar</packaging>
<properties>
<spring-version>4.1.4.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.21</version>
</dependency>
// and more dependencies
</dependencies>
</project>
and here is the Parent POM
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xyz</groupId>
<artifactId>Foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>FooBatch</module>
<module>FooEJB</module>
<module>FooEAR</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-compiler-plugin/ -->
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<!-- skips surefire tests without skipping failsafe tests. Property
value seems to magically default to false -->
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<argLine>${jacoco.agent.arg}</argLine>
</configuration>
</execution>
</executions>
<configuration>
<!-- skips failsafe tests without skipping surefire tests. Property
value seems to magically default to false -->
<skipITs>${skipITTests}</skipITs>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Try to add the following to your parent pom.
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Include Scala project in Java Maven dependency

Im new to scala, Im trying to include scala project into my java project.
I have separate scala and java project and Im using scala in java.
It is working fine when I add scalaTest to my java project build path. Is there any way I can edit my pom.xml that automatically refer the scalaTest project while maven build?.
I have added scala dependency in my pom.xml
Or is there any way that I can include both java and scala files into my java project and run ?
If you the following pom.xml you should be able to compile Java and Scala sources from within the same project via mvn.
<?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>org.example</groupId>
<artifactId>javaScala</artifactId>
<version>1.0-SNAPSHOT</version>
<name>javaScala</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Answers to my own question
Add Scala project in class path (buildpath) of java project.
Export Scala project as jar and import to your java project.
To include both java and scala in single java project, follow this.
Include java files into - src/main/java
Include scala files into - src/main/scala
Use this pom.xml
Download project from https://github.com/rthoma24/Java-Scala.git
maven commands
mvn compile
mvn exec:java -Dexec.mainClass=main.java.Test
<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>ted-gao</groupId>
<artifactId>scala-java-mix</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Scala-Java mixture</name>
<description>Showcase mixing Scala and Java</description>
<packaging>jar</packaging>
<build>
<plugins>
<!-- ensure that we use JDK 1.6 -->
<plugin>
<inherited>true</inherited>
<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.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<!-- Run scala compiler in the process-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) compile phase -->
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<!-- Run scala compiler in the process-test-resources phase, so that dependencies on
scala classes can be resolved later in the (Java) test-compile phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<!-- Add src/main/scala to source path of Eclipse -->
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
<!-- Add src/test/scala to test source path of Eclipse -->
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- to generate Eclipse artifacts for projects mixing Scala and Java -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<projectnatures>
<projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
<projectnature>org.eclipse.jdt.core.javanature</projectnature>
</projectnatures>
<buildcommands>
<buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
</buildcommands>
<classpathContainers>
<classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
</classpathContainers>
<excludes>
<!-- in Eclipse, use scala-library, scala-compiler from the SCALA_CONTAINER rather than POM <dependency> -->
<exclude>org.scala-lang:scala-library</exclude>
<exclude>org.scala-lang:scala-compiler</exclude>
</excludes>
<sourceIncludes>
<sourceInclude>**/*.scala</sourceInclude>
<sourceInclude>**/*.java</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
<!-- When run tests in the test phase, include .java and .scala source files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<includes>
<include>**/*.java</include>
<include>**/*.scala</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
If mvn install/compile gives any error, delete your m2 directory and try again.

Maven trying to deploy the same artifact twice

I'm using Maven to build my project, but when I run the command mvn clean package deploy, it tries to deploy the artifact twice. I have the build-helper-maven-plugin plugin configured to attach an ear file that I create using a custom plugin.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.artifactId}-${project.version}.ear</file>
<type>ear</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
When I disable build-helper-maven-plugin, the remaining artifact (only the pom) is uploaded only once.
What should I do to let Maven deploy the extra ear file only once?
Erates
EDIT
<?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>my.group.id</groupId>
<artifactId>my.artifact.id</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>My Project</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<scm>
<!-- Config -->
</scm>
<distributionManagement>
<repository>
<!-- Config -->
</repository>
<snapshotRepository>
<!-- Config -->
</snapshotRepository>
</distributionManagement>
<dependencies>
<!-- My Dependencies here -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeGroupIds>my.group.ids.that.need.to.be.included</includeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>my.group.id</groupId>
<artifactId>my.custom.plugin</artifactId>
<version>1.0.1</version>
<configuration>
<params>
<!-- My params -->
</params>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>my-custom-goal</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Release Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.4</version>
<configuration>
<goals>clean package deploy</goals>
<tagBase>https://my.tagbase</tagBase>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.artifactId}-${project.version}.ear</file>
<type>ear</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<modules>
<!-- My Modules -->
</modules>
</project>
First you are using module and trying to do weird things in your parent pom (dependency-plugin, build-helper etc.). In a parent there should never be an execution like you have in your pom. You should make the appropriate configuration/execution within the appropriate modules cause this definition will be inherited of all childs.
Would you like to create an ear file? Than you should use packaging ear and your ear file will simply being deployed by using mvn deploy.
Furthermore you seemed to misunderstand the life cycle cause if you call:
mvn clean package deploy
this can be reduced to:
mvn clean deploy
cause the package life cycle is part of deploy so i recommend to read the life cycle information.

Maven2 Binding to a custom Phase

I have a custom plugin that is defined using the 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pram.plugintest</groupId>
<artifactId>pram.plugintest</artifactId>
<packaging>maven-plugin</packaging>
<version>1.1-SNAPSHOT</version>
<name>pram.plugintest Maven Mojo</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.3</version>
<configuration>
<goalPrefix>blah</goalPrefix>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Running
mvn blah:touch
Creates a text file in the target directory as expected. I now create a lifecycles.xml file in the resources directory specified in the pom
<lifecycles>
<lifecycle>
<id>touch</id>
<phases>
<phase>
<id>package</id>
<executions>
<execution>
<goals>
<goal>touch</goal>
</goals>
</execution>
</executions>
</phase>
</phases>
</lifecycle>
</lifecycles>
In another maven project, I would like to bind the running of mvn blah:touch to an execution task similar to this
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>test1</id>
<phase>blah:touch</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass>
</configuration>
</execution>
</executions>
</plugin>
...
However running this creates the text file but doesn't attempt to run org.sonatype.mavenbook.weather.Main
Is this the correct approach?
Ultimately what I would like is to have multiple execution sections in the exec-maven-plugin that are not bound to the default phases. Logically it would look like this
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>test1</id>
<phase>blah:touch</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>mainClass=org.sonatype.mavenbook.weather.Main</mainClass>
</configuration>
</execution>
<execution>
<id>test2</id>
<phase>blah:touch2</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>mainClass=org.sonatype.mavenbook.weather.SomeOtherClass</mainClass>
</configuration>
</execution>
</executions>
</plugin>
...
So if I run mvn blah:touch then org.sonatype.mavenbook.weather.Main will be executed and if I run mvn blah:touch2 then org.sonatype.mavenbook.weather.SomeOtherClass will be executed instead.
It seems like it should be straightforward to do but there's nothing in the documentation that seems to point out how to do this.
You can not use the exec-maven-plugin for this and you do not need the lifecycle.xml if you only would like to execute your plugin during a build.
To execute your plugin during a specific Maven phase, you simply have to add
<plugins>
<plugin>
<groupId>your.group.id</groupId>
<artifactId>your.artifact.id</artifactId>
<executions>
<execution>
<id>unique-execution-id</id>
<goals>
<goal>the.goal.of.your.plugin</goal>
</goals>
<phase>maven.phase</phase>
<configuration>
....
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Please specify the goal in the goal element without the prefix.
Did you read http://www.sonatype.com/books/mvnref-book/reference/writing-plugins-sect-plugins-lifecycle.html?

Categories

Resources