How can I deploy only the pom file to my snapshot

2019-04-21 09:59发布

问题:

I would like to be able to deploy only the POM artifact (file) without the main artifact (JAR, WAR, etc), when running mvn deploy and version is a SNAPSHOT version.

Why?

We several developers working on multiple Maven projects. We have a Hudson server with a job per Maven project and version (e.g. foo-1.2, foo-1.3). Each job builds the project and deploys it to a Nexus server (upon success). Maven projects under development are marked as such by using -SNAPSHOT postfix in the version. For example: 1.2-SNAPSHOT, 1.3-SNAPSHOT.

Here's a sample scenario how a developer work is damaged due to this architecture.

Assume two Maven projects: foo-core and foo-webapp, both at version 1.2-SNAPSHOT.

  • Developer A is working on foo-core, made several changes and compiled it.
  • Developer A continues to work, but on foo-webapp.
  • Developer B started working and changing foo-core. It commits his work and pushes it to the SCM.
  • Hudson is triggered by SCM; Builds foo-core and deploys it to the snapshot repository in Nexus.
  • Developer A is running mvn install on foo-webapp. Maven is checking with Nexus, and finds that there is a newer version of foo-core in Nexus. It downloads it (filled with developer B changes) and than it fails compilation, since the changes made by developer A are not in the jar located in the local repository. The download overrides the file installed there by developer A.

Existing Solutions

I looked into maven-deploy-plugin, but this plugin deploys all artifacts attached to the project. If they had a way to configure which artifacts to deploy, it would have been great.

Question: Is there any way to solve this without resorting to writing my own deploy plugin, based on maven-deploy-plugin?

回答1:

Basically to the -Dfile parameter, instead of the artifact, pass the pom.xml, but just doing this is not enough. You'll also need to supply the parameters -DgroupId and -DartifactId of the project. Run the command and yay! mvn deploy won't give you any issues now. Here's a sample deploy command :

$ mvn deploy:deploy-file -DpomFile=pom.xml -Dfile=./pom.xml -DgroupId=my.group.id -DartifactId=artifact-id -DrepositoryId=bigdata-upload-snapshots -Durl=http://maven.mymaven.com/content/repositories/snapshots/

A prerequisite for this is that the repository be added in your settings.xml



回答2:

I never heard of such a possibility and also would be very astonished if that would be possible. As the pom and the resulting artifact are some kind of unit it would make no scence (to me) to deploy only parts of them.

Nevertheless you should consider to make a separate pom project which specified dependencies and plugins you might want to use on your JAR/WAR projects like this:

<groupId>foo.bar</groupId>
<artifactId>my-pom</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

and then inherit that pom project by your JAR/WAR projects like this:

<parent>
    <groupId>foo.bar</groupId>
    <artifactId>my-pom</artifactId>
    <version>1.0.0</version>
</parent>

This is called project inheritance. You can change and deploy your pom project independent of the "child" artifacts.

EDIT after reading motivation:

As I understand you want to prevent maven to resolve SNAPSHOT artifacts from a repository (so that local version won't be overwritten). Have you ever tried to use the mvn -nsu option (see mvn -help)?

-nsu,--no-snapshot-updates             Suppress SNAPSHOT updates

I never tried it but found this reported issue. Nevertheless I would give it a try (as the issue is not commented yet).