29

I need to deploy a custom jar to Artifactory along with the jar generated from my Java project. Currently the only method I can find is through command line goal using:

mvn deploy:deploy-file -DgroupId=<group-id> \
  -DartifactId=<artifact-id> \
  -Dversion=<version> \
  -Dpackaging=<type-of-packaging> \
  -Dfile=<path-to-file> \
  -Durl=<url-of-the-repository-to-deploy>

Is there a way of including this in the pom file? As a plugin or something?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user171943
  • 1,325
  • 3
  • 12
  • 18

4 Answers4

28

Sure. Just define an execution of the maven-deploy-plugin:deploy-file goal bound to the deploy phase, configured with your values. When deploying your project, this execution will be invoked and the JAR will be deployed.

<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>
                <file><!-- path-to-file --></file>
                <url><!-- url-of-the-repository-to-deploy --></url>
                <groupId><!-- group-id --></groupId>
                <artifactId><!-- artifact-id --></artifactId>
                <version><!-- version --></version>
                <packaging><!-- type-of-packaging --></packaging>
            </configuration>
        </execution>
    </executions>
</plugin>

Note that you will probably need to add a repositoryId also. This is the server id to map on the <id> under the <server> section of the settings.xml.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • But why does it not work if `deploy` is removed? – Jean-Louis Jouannic Sep 27 '16 at 09:53
  • @Jean-LouisJouannic Because the execution will not be bound to a phase, and so it won't get executed by default. Specifying `deploy` allows this execution to be executed when `mvn deploy` is run. – Tunaki Sep 27 '16 at 09:55
  • 1
    I agree, but removing `deploy` and calling `mvn deploy:deploy-file` should just work, shouldn't it? But is does not and I don't get why. – Jean-Louis Jouannic Sep 27 '16 at 14:51
  • 6
    @Jean-LouisJouannic No, because it's defined within an `` block, so you need to execute that specific execution, which doesn't happen with `mvn deploy:deploy-file` (it creates a default different execution). If you want to execute a specific execution from the command line, you'll need `mvn deploy:deploy-file@deploy-file`, i.e. `@` the id of the configured execution, and make sure you're using at least Maven 3.3.1 ([see also](http://stackoverflow.com/q/2192660/1743880)). – Tunaki Sep 27 '16 at 14:52
  • How can I change automatically from snapshot to release ? – zombi_man Nov 14 '16 at 15:00
  • @zombi_man if you need to differentiate between snapshots and releases, you're probably going at it the wrong way with this solution. It means you're doing this dynamically, and you should use `deploy` instead on a concrete Maven project, with different `snapshotRepository` and `repository` under the `distributionManagement`. The difference is something Maven does built-in with the `deploy` goal, but not with `deploy-file` since this is more of a static set-up. Whatever you're generating, consider turning it into a Maven project. – Tunaki Nov 14 '16 at 15:22
  • @Tunaki, ok, maybe you can help me with right way: I want to share my docker images and docker-compose.xml files. I uploaded docker images to the docker hub, but where I need to store compose? + I want to change version on this compose ... – zombi_man Nov 14 '16 at 15:32
  • @zombi_man I think you'd be better asking a new question for this, describing your situation and current issue; I'm not well versed with Docker. – Tunaki Nov 14 '16 at 15:54
  • Do you really have to specify the `groupId`, `artifactId` and `version` on the `configuration` tag? Won't they be picked up from the once on the `project` tag? – Lii Feb 02 '17 at 16:32
  • @Lii No, mainly because this goal is largely intended to be used directly on the command-line to deploy things that aren't Maven projects. But they will be picked up from the POM file if it is specified with the [`pomFile`](https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html#pomFile) parameter and if it contains the info. – Tunaki Feb 02 '17 at 16:34
  • @Tunaki: I was confused about which of my questions your "no" refers to. But not I'm almost certain that you mean: "No, these parameters will *not* be picked up from the *containing* POM file. They must be specified manually (or picked up from the POM that is specified in the *`configuration` tag*)." – Lii Feb 03 '17 at 09:42
  • @Lii Ah sorry for the confusion, this is indeed the interpretation I meant. – Tunaki Feb 03 '17 at 10:53
1

The Working with Maven section in the Artifactory manual covers this topic in detail, specifically the part about deploying artifacts.
In addition you can watch the screencast about setting Artifactory as a Maven repository.

To deploy build artifacts through Artifactory you must add a distributionManagement element to your project pom file with the URL of a target local repository to which you want to deploy your artifacts. In addition you will need to configure the Artifactory server credentials in your settings.xml file.
Artifactory can help with generating both the distributionManagement snippet and the settings.xml (see more info in the links I provided above).

Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
  • The page you mentioned says that "Remember that you can not deploy build artifacts to remote, so you should not use them in a deployment element." does this mean the selected answer above is wrong for artifactory ? – thedrs Mar 23 '20 at 14:15
  • No. A "remote" repository in Artifactory is a repository which proxies another repository (mostly public ones like Dockerhub, Maven Central etc. and caches remote artifacts locally. This type of repository in Artifactory cannot be used for deploying your own artifacts, for this you should use local repositories. – Dror Bereznitsky Mar 23 '20 at 14:19
0

You can upload custom maven settings.xml to TeamCity, where you must specify distributionManagement and server as stated on this documentation page. After this, if you change your maven build step to use uploaded settings, deployment will be done by simply adding deploy gial to set of executed goals in this step.

Oleg Rybak
  • 1,651
  • 1
  • 14
  • 27
  • I already have a custom maven settings.xml where i specify the server and proxy settings etc. but How do I include deploy:deploy-file mojo as a step? – user171943 Feb 02 '16 at 16:46
  • If you include distributionManagement tag in either settings.xml or pom.xml, you don't need to execute deploy:deploy-file goal. Create a maven build step, use your settings.xml and add 'deploy' to goals. – Oleg Rybak Feb 02 '16 at 16:48
0

I personally don't think declaring it in POM is a good approach. For example, if you have a multi-module Maven project consisting of children POMs inheriting from parent POM/super POM and the custom jar to upload is contained in only 1 of the child module, then you will have to explicitly declare the configuration in parent POM (since mvn deploy is normally used to execute the superpom) and declare every other child POM to skip the execution of the configuration, which clutters the POM structure.

A better approach would be to write a script, and link it to your run configurations of deploy goal since the deploy:deploy-file goal is at the end of the build lifecycle.

atjua
  • 541
  • 1
  • 9
  • 18