Deploying Ant-generated jar to Nexus using maven-deploy-plugin - maven

I have a multi-module project with war and ear module. In war's pom I'm generating jar using maven-antrun-plugin. Now I'm trying to deploy that jar to my Nexus using maven-deploy-plugin.
Here's my war's 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>com.example</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>test-war</artifactId>
<packaging>war</packaging>
<properties>
...
</properties>
<dependencies>
...
</dependencies>
<!-- <distributionManagement>
<repository>
<id>nexus-releases</id>
<url><nexus_releases_repo_url></url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url><nexus_snapshots_repo_url></url>
</snapshotRepository>
</distributionManagement> -->
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>ant-build</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<ant antfile="<path_to_build_xml>" target="run" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>deploy-file</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>nexus-snapshots</repositoryId>
<file>fileToDeploy.jar</file>
<url>http://<path_to_my_repo></url>
<groupId>${project.parent.groupId}</groupId>
<artifactId>${project.parent.artifactId}-tmp</artifactId>
<version>${project.parent.version}</version>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run clean deploy on parent pom, I'm getting the following error:
Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project test: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]
Ok, so I've added distributionManagement section in war's pom, but I'm getting the same error. If I run the same command on war module, there's no error, but there's also war file deployed on Nexus next to jar file.
Is there a way to deploy just jar file using deploy command?

Related

How to access a plugin in a maven child module from the parent module

I have a maven multi-module setup, which includes the fabric8 plugin in a profile defined in a child module to build a Docker image if the profile is active, see below
Root pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>control</name>
<packaging>pom</packaging>
<modules>
<module>parent</module>
<module>docker</module>
<module>...</module>
<module>...</module>
<module>...</module>
</modules>
</project>
Child 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>com.mycompany</groupId>
<artifactId>docker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<profiles>
<profile>
<id>Docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.34.1</version>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>${aws.version}</version>
</dependency>
</dependencies>
<configuration>
<registry>${aws.ecr.registry}</registry>
<images>
<image>
<name>${aws.ecr.registry}/${aws.ecr.repository}:${project.artifactId}-${project.version}</name>
<build>
<contextDir>${project.build.directory}/docker.tmp/</contextDir>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-build</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>docker-push</id>
<phase>install</phase>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Runing a normal build, i.e. mvn clean install -PDocker completes successfully, but if I call one of the docker goals from the parent module directly, e.g. mvn -PDocker docker:help it fails with this error
No plugin found for prefix 'docker' in the current project and in the plugin groups
Running the same command from the docker directory completes successfully.
What changes are needed so the command completes successfully in the root directory?

How to generate a SINGLE jar contains /src/main/java and /src/test/java

I have created a maven project and trying to generate a SINGLE jar file should contain both /src/main/java & /src/test/java.
But it is generating two SEPARATE jar files. Please let me know, how can I achieve this in maven?
generated jar files:
test-0.0.1-SNAPSHOT.jar
test-0.0.1-SNAPSHOT-tests.jar
my 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>com.testmaven</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
You cannot access test classes from application code. So no maven plugin will help you to add src/main/java and src/test/java in a single executable jar.
If at all you want to access the main classes from the test project, check the answer similar to your question on stackoverflow here:
How can I include test classes into Maven jar and execute them?
I have tried with below approach and it worked.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/test/java/</source>
</sources>
</configuration>
</execution>
</executions>

maven Return code is: 409, ReasonPhrase: Conflict

I have problem to deploy my multi-module python project with following structure
parent pom.xml # common pom for others project
project/
pom.xml # project pom
common_features/
sub-project-1/pom.xml # sub project 1 pom
sub-project-2/pom.xml # sub project 2 pom
For deployment I use maven
Settings.xml
General maven setting use for build
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- others options -->
<servers>
<server>
<id>artifacts-SNAPSHOTS</id>
<username>user</username>
<password>password</password>
</server>
</servers>
<!-- others options -->
</settings>
parent pom.xml
In this case we have the same URL for SNAPSHOT and RELEASE repository
<!-- others options -->
<groupId>com.project.common.group.id</groupId>
<artifactId>project-pom</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<!-- others options -->
<distributionManagement>
<repository>
<id>artifacts-RELEASES</id>
<url>${dist.repository.releases}</url>
</repository>
<snapshotRepository>
<id>artifacts-SNAPSHOTS</id>
<url>${dist.repository.snapshots}</url>
</snapshotRepository>
</distributionManagement>
<!-- others sections-->
<properties>
<dist.repository.releases>${artifacts.repository.url}</dist.repository.releases>
<dist.repository.snapshots>${artifacts.repository.url}</dist.repository.snapshots>
<artifacts.repository.url>https://domain/artifactory/mvn-repo-dev</artifacts.repository.url>
</properties>
project pom.xml
There you can see that project consists two modules
sub-project-1
sub-project-2
Parent is general parent.
Also I sue maven assembly plugin in order to deploy project into artifactory
<!-- anothers options -->
<parent>
<groupId>com.project.common.group.id</groupId>
<artifactId>project-pom</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.project.group.id</groupId>
<artifactId>project-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>common_features/sub-project-1</module>
<module>common_features/sub-project-2</module>
</modules>
build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>deploy/artifact-description.xml</descriptor>
</descriptors>
<tarLongFileMode>posix</tarLongFileMode>
</configuration>
<executions>
<execution>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>2.6.1</version>
<inherited>false</inherited>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>publish</goal>
</goals>
<configuration>
<publisher>
<contextUrl>https://domain/artifactory</contextUrl>
<repoKey>mvn-repo-dev</repoKey>
<snapshotRepoKey>mvn-repo-dev</snapshotRepoKey>
<publishArtifacts>true</publishArtifacts>
<publishBuildInfo>true</publishBuildInfo>
<username>deployUser</username>
<password>deployPwd</password>
</publisher>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
artifact-description.xml
I want to deploy only some section therefore I define the following deployment file. With this I ensure that into package will be deploy only common features and project specific scripts/codes
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly- plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>${distributionId}</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>project/${subProjectName}/</outputDirectory>
<directory>${basedir}/..</directory>
<fileMode>0755</fileMode>
<lineEnding>unix</lineEnding>
<includes>
<include>/*.*</include>
<include>/sub-${subProjectName}/*</include>
</includes>
<excludes>
<exclude>**/*.pyc</exclude>
<exclude>**/*.xls</exclude>
<exclude>**/*.xlsx</exclude>
<exclude>**/pom.xml</exclude>
<exclude>**/target</exclude>
<exclude>**/deploy</exclude>
<exclude>**/.venv</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
sub-project-1 pom.xml
This is only simple pom without others plugins and build options
<parent>
<groupId>com.project.group.id</groupId>
<artifactId>project-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
<groupId>com.project.group.id</groupId>
<artifactId>sub-project-1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<subProjectName>project-1</subProjectName>
</properties>
sub-project-2 pom.xml
The same pom.xml as for sub-project-1
<parent>
<groupId>com.project.group.id</groupId>
<artifactId>project-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
<groupId>com.project.group.id</groupId>
<artifactId>sub-project-2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<subProjectName>project-2</subProjectName>
</properties>
Now I want to build and upload only sub-project-1 it into artifactory and therefore I will use the following maven command:
mvn -e -B -U -X clean deploy --projects :sub-project-1 -DskipIntegrationTests=false -DskipCoverageReport=false
But then following error occur:
Caused by: org.apache.maven.wagon.TransferFailedException: Failed to transfer file: https://domain/artifactory/mvn-repo-dev/com/project/group/id/project/project-1/1.0.0-SNAPSHOT/sub-project-1-1.0.0-20190723.081454-1.pom. Return code is: 409, ReasonPhrase: Conflict.
at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.put(AbstractHttpClientWagon.java:627)
at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.put(AbstractHttpClientWagon.java:541)
at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.put(AbstractHttpClientWagon.java:523)
at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.put(AbstractHttpClientWagon.java:517)
at org.apache.maven.wagon.providers.http.AbstractHttpClientWagon.put(AbstractHttpClientWagon.java:497)
at org.eclipse.aether.transport.wagon.WagonTransporter$PutTaskRunner.run(WagonTransporter.java:644)
at org.eclipse.aether.transport.wagon.WagonTransporter.execute(WagonTransporter.java:427)
at org.eclipse.aether.transport.wagon.WagonTransporter.put(WagonTransporter.java:410)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$PutTaskRunner.runTask(BasicRepositoryConnector.java:510)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:350)
... 32 more
Now I am not sure what is wrong and I spend a lot of hours of investigation this issue but nothing found. Please do you have some suggestions what is wrong here? Thanks a lot
I found where is the problem. Within parent pom.xml there was the following plugin which change artifactId. I do not know why I got error 409 but finally this was a reason:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>pre-clean</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
project.getModel().setArtifactId(project.properties["someProperty"].replaceAll('_',
'-'))
project.getArtifact().setArtifactId(project.properties["someProperty"].replaceAll('_',
'-'))
</source>
</configuration>
</execution>
</executions>
</plugin>

Jenkins stop build on unit test first failure

I have a maven job running in jenkins. This maven project runs soap project which contains testcases. The pom.xml of this maven project is configured as
<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>src.main.resources</groupId>
<artifactId>soapui-maven2-plugin</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven 2 soapUI Sample</name>
<url>http://maven.apache.org</url>
<pluginRepositories>
<pluginRepository>
<id>smartbear-sweden-plugin-repository</id>
<url>http://www.soapui.org/repository/maven2/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>com.github.redfish4ktc.soapui</groupId>
<artifactId>maven-soapui-extension-plugin</artifactId>
<version>4.6.4.2</version>
<configuration>
<runnerType>OSS</runnerType>
<projectFile>Dev-Offline-soapui-project.xml</projectFile>
<junitReport>true</junitReport>
<outputFolder>${project.build.directory}/tatunka-reports</outputFolder>
<testSuite>SmokeTestSuite</testSuite>
<testSuiteProperties>
<properties>
<property>serviceEndpoint=${serviceEndpoint}</property>
</properties>
</testSuiteProperties>
<skipAfterFailureCount>1</skipAfterFailureCount>
</configuration>
<executions>
<execution>
<configuration>
<junitReport>true</junitReport>
</configuration>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.smartbear.soapui</groupId>
<artifactId>soapui-maven-plugin</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
And in jenkins goal and options is given as
test -DserviceEndpoint=http://localhost:8080/DAMService/solutionid -DskipTests=true
What I want is the jenkins to stop running other testcases if any testcase fails.
For example, i have 9 testcases and 4th testcase fails then remaining 5 testcases should not be executed.
I tried to give
<skipAfterFailureCount>1</skipAfterFailureCount>
but no result.
And even tried giving
-DskipTests=true option as build arguement.
And also tried with goal verify. But could not achieve to stop the running of testcases.
Please, let me know if any more information is needed.
Add <testFailureIgnore>true</testFailureIgnore>under plugin configuration and try

Artifact has not been packaged yet - maven-dependency-plugin

When I build a multi module maven project(using mvn clean compile) where one dependency(part of the build reactor) is copied into another using dependency:copy, then maven complains with the below error.
Artifact has not been packaged yet. When used on reactor artifact, copy should be executed after packaging: see MDEP-187 is thrown
This is perfectly fine, Maven can't copy the dependent jar because it has not been packaged yet and the dependency has to be resolved from the local project and not from the repository.
Lets say project A is being copied into project B using the dependency:copy goal.
Now if I import the projects into eclipse,with the lifecycle mapping set in such a way that the maven-jar-plugin is executed on project A(which means that A is packaged), still project B complains with the same error. How can I get rid of this.I can't ignore dependency:copy in the m2e lifecycle as it's a crucial phase in the build.
ProjectA's pom file :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>projecta</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.coderplus.tests</groupId>
<artifactId>projectparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<versionRange>[2.4,)</versionRange>
<goals>
<goal>jar</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Project B's pom file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>projectb</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.coderplus.tests</groupId>
<artifactId>projectparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-plugins</id>
<phase>compile</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>projecta</artifactId>
<version>${project.version}</version>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/classes/jars</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.8,)</versionRange>
<goals>
<goal>copy-dependecies</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute/>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
and the Parent pom wrapping these 2 modules
<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.coderplus.tests</groupId>
<artifactId>projectparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>projecta</module>
<module>projectb</module>
</modules>
</project>
I need a ProjectB's jar to contain ProjectA's jar in the jars directory(This is required by a custom class-loader part of a home-grown framework)
With m2eclipse, the artifactItems or dependencies will be resolved to the outputDirectory of the corresponding workspace project if the Resolve dependencies from workspace Projects setting is turned on. This is the root cause for the error in my question.
I ended up forking the existing m2e configurator for this plugin so that it can fetch these dependencies/artifactItems from the respository instead of picking it from the workspace project's outputDirectories .
Though this works with few tests which I had done, it is pretty much a work in progress and in case someone wants to help or contribute, the GitHub URL is
https://github.com/coderplus/m2e-maven-dependency-plugin

Resources