Just put the plugin exists
.
When I started my search for this, my first idea would be to solve it through shell script itself. I would get the version through the help:evaluate
, put it in a variable and find out if it already existed on the server.
How to do that? Well, here comes the complication... But, a priori, knowing that my artifact server is under a fixed name (let’s call it http://artifactory
), could check if the artifact was there:
curl http://artifactory/libs-release/group/id/artifatic-id/M.m.i/
# \________________/ \__________/ \______/ \__________/ \___/
# | | | | |
# | nome do repositório | meu artifactId |
# | | |
# autoridade do servidor meu groupId minha versão em questão
An unsuccessful return (no artifactory
, at least), when there is no such version of the artifact, it occurs with a 404 code and the following output:
{
"errors" : [ {
"status" : 404,
"message" : "{\"error\":\"children items not found on all virtual repos\"}"
} ]
}
Already a successful, beyond the code 200, a page with the links to download the pom
and the artifact generated:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head><title>Index of libs-release/group/id/artifatic-id/M.m.i</title>
</head>
<body>
<h1>Index of libs-release/group/id/artifatic-id/M.m.i</h1>
<pre>Name Last modified Size</pre><hr/>
<pre><a href="../">../</a>
<a href="artifatic-id-M.m.i.jar">artifatic-id-M.m.i.jar</a> 10-Jul-2019 20:59 25.82 KB
<a href="artifatic-id-M.m.i.pom">artifatic-id-M.m.i.pom</a> 25-Jul-2019 17:28 5.97 KB
</pre>
<hr/><address style="font-size:small;">Artifactory/6.3.0 Server at artifactory Port 80</address></body></html>
But luckily I would have to worry about rescuing which repositories serve dependencies exist via help:evaluate
. The fact of possibly containing inheritance of pom
makes it difficult to try to solve this via grep
or other simple agnostic textual analysis tools from Maven.
While searching for a way to list the repositories, I came across the plugin org.honton.chas:exists-maven-plugin
. Its use is very simple: configures whether it should be used with the Goal local
or remote
and have it executed. In this case, I simulated the following in a toy design to avoid replacing my artifact (which is SNAPSHOT
) in the local repository:
<build>
<pluginManagement>
<plugins>
<!-- outros plugins -->
<plugin>
<groupId>org.honton.chas</groupId>
<artifactId>exists-maven-plugin</artifactId>
<version>0.1.0</version>
<executions>
<execution>
<goals>
<goal>local</goal>
<!-- para repositórios remotos, mude o goal para
<goal>remote</goal>
-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- outros plugins -->
<plugin>
<groupId>org.honton.chas</groupId>
<artifactId>exists-maven-plugin</artifactId>
<version>0.1.0</version>
</plugin>
</plugins>
<!-- outras opções de build -->
</build>
And I ran the following command:
Note that as I’m playing with an artifact SNAPSHOT
, I needed to turn off the configuration exists.skipIfSnapshot
with the option -Dexists.skipIfSnapshot=false
$ mvn -pl :artifact-id install -Dexists.skipIfSnapshot=false
When I run it clean, it follows the installation normally:
$ mvn -pl :artifact-id install -Dexists.skipIfSnapshot=false
....
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ artifact-id ---
[INFO]
[INFO] --- exists-maven-plugin:0.1.0:local (default) @ artifact-id ---
[INFO] group.id:artifact-id:M.m.i-SNAPSHOT does not exist
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ artifact-id ---
[INFO] Installing ...
When I run again:
$ mvn -pl :artifact-id install -Dexists.skipIfSnapshot=false
....
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ artifact-id ---
[INFO]
[INFO] --- exists-maven-plugin:0.1.0:local (default) @ artifact-id ---
[INFO] setting maven.install.skip=true
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ artifact-id ---
[INFO] Skipping artifact installation
This also applies to projects within a reactor, as the above example demonstrates.
It is important to note that, to use the exists-maven-plugin
, it is necessary to run with the Maven version at least 3.5.0. Running in earlier versions has a result much strange. If you simply ask if the artifact exists (for example, calling the mojo org.honton.chas:exists
) and just that, everything works fine. However, when putting to run together install
, a great stacktrace is printed without major tips.
I see this if you use the maven-wrapper
I would like to know how I can improve my question, since it has not shown itself worthy of being closed. I would be very grateful for the feedback
– Jefferson Quesado