Can I remove jar from local repository without pom.xml? - java

I install my jar to local repository by command:
mvn install:install-file -Dfile=aaa.jar -DgroupId=bbb -DartifactId=ccc -Dversion=1.0 -Dpackaging=jar
Now I want remove it from repository. I try command:
mvn dependency:purge-local-repository -DmanualInclude=bbb-ccc
But get error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:purge-local-repository (default-cli): Goal requires a project to execute but there is no POM in this directory (...). Please verify you invoked Maven from the correct directory. -> [Help 1]
Then I create pom.xml with data:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>bbb</groupId>
<artifactId>ccc</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>${project.packaging}</packaging>
<file>aaa.jar</file>
</properties>
</project>
Now I can remove jar from repository. But can I remove by command line without creating pom.xml?
I'm using 3.2.5 (windows x64).

The dependency:purge-local-repository goal, up to the current version 2.10, needs to be executed on a Maven project:
Requires a Maven project to be executed.
One of the purpose of this goal is to remove from the local repository the dependencies of a Maven project, so it needs one to execute. This explains the error you have.
However, with this plugin, it is possible to specify a manualInclude parameter, which will remove any dependency specified as groupId:artifactId:version or all versions of groupId:artifactId or even everything under a groupId. Therefore, the plugin could be updated to not require a Maven project to be executed.
I went ahead, created the JIRA issue MDEP-537 to track this addition and will fix this for version 3.0.0.

As Tunaki mentioned in his answer, he requested MDEP-537 for the Dependency plugin to be able to purge without a Maven project. This has been implemented since version 3.0.0.
If you only run mvn dependency:purge-local-repository, you are likely to run an older version. E. g. my Maven 3.6.0 comes with Dependency plugin version 2.8.0.
So you should run a specific version of the plugin:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.0:purge-local-repository -DmanualInclude=...

Related

mvn versions:set fail after a build failure

My snapshot version looks like 2.7.644-SNAPSHOT
So pom.xml normally look like
<parent>
<groupId>com.orderManager</groupId>
<artifactId>parent-pom</artifactId>
<version>2.7.644-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
But after a failed release my pom.xml file looks like
<parent>
<groupId>com.orderManager</groupId>
<artifactId>parent-pom</artifactId>
<version>2.7.644.344</version>
<relativePath>..</relativePath>
</parent>
When I try to correct all versions by running mvn versions:set -DnewVersion=2.7.644-SNAPSHOT, I get an error
mvn is trying to find the failed build jars in a remote repo for some reason.
I'm just trying to change the version names in pom.xml, there's no reason to download jars. Is there a way to run mvn versions:set without downloading current version's jars?
Simply before any plugin will be executed on project, Maven need to load your project and resolve needed dependencies.
In case when corrupt your project for any reason, you have options:
restore previous/correct version of code from scm - if you have
fix manually problematic versions in poms

Excluding some dependency from mvn dependency:resolve

In my pom.xml of com.test:Service:1.0, I have a dependency on some local jar: com.test:Parser:1.0
I want to resolve all dependencies except for it, since I install it manually to my local maven. Resolve command:
mvn -B dependency:resolve -DincludeParents=true
But it fails on:
[ERROR] Failed to execute goal on project Service: Could not resolve
dependencies for project com.test:Service:jar:1.0: Could not find
artifact com.test:Parser:jar:1.0 in central
(https://repo.maven.apache.org/maven2)
Then I tried to add the options
-DexcludeGroupIds=com.test -DexcludeArtifactIds=Parser
but am still getting the same error. Am I misusing the options?
Reference: http://maven.apache.org/plugins/maven-dependency-plugin/resolve-mojo.html
I'm using 3.8.6 and it looks like go-offline works with exclude*, but resolve does not:
mvn dependency:go-offline -DexcludeGroupIds=com.test
According to: https://maven.apache.org/plugins/maven-dependency-plugin/index.html
dependency:go-offline tells Maven to resolve everything this project is dependent on (dependencies, plugins, reports) in preparation for going offline.
dependency:resolve tells Maven to resolve all dependencies and displays the version.
This seems to simply be a bug. See
https://issues.apache.org/jira/browse/MDEP-568
https://github.com/apache/maven-dependency-plugin/pull/2
The solution, as proposed in the above threads, is to use a different library: https://github.com/qaware/go-offline-maven-plugin
In your pom.xml add the plugin:
<plugin>
<groupId>de.qaware.maven</groupId>
<artifactId>go-offline-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<dynamicDependencies>
</dynamicDependencies>
</configuration>
</plugin>
Then use the command mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies with the required options.

Maven dependency on other Maven projects

I have multiple maven projects, to add dependencies between them I just add it to the POM, for example :
<dependency>
<groupId>com.company</groupId>
<artifactId>XMLManagement</artifactId>
<version>1.0</version>
</dependency>
I am working with eclipse, and everything runs and works great until I try to release a jar.
**Problem1: ** Maven throws an error because it can't find the jars of the other maven projects it depends on in the local repository,
and this means that I have to figure out myself the dependencies between the projects, and go to each and every project in the right order and run "mvn clean install"
This does not seem logical since that is the main purpose of maven, there must be a way to tell maven to do it himself.
**Problem2: ** If project A depends on B that depends on C, and I want to use C in A then in eclipse its enough that I added B in the POM of A and it works, but when I run "mvn clean install" maven throws an error that it is missing dependency of C.
This means I have to add the dependency between A and C, which doesn't make sense because in eclipse I already see it under "Maven Dependencies", so if eclipse recognizes it why mvn clean install doesn't?
Note that I am able to produce the jar I need at the end, but only after a lot of hard work as described above.
I know I can use something like nexus or artifactory, but it's an overkill for me and I want to be able to do it in local repository.
I am looking for the proper way to do it, any suggestions?
What you need is a top-level pom that lists each of the things you want build as modules. In maven jargon, this is known as a reactor build.
Something like:
<groupId>this.that</groupId>
<artifactId>build.root</artifactId>
<name>A name</name>
<packaging>pom</packaging>
<modules>
<module>../a.b</module>
<module>../a.c</module>
Try this, find your file settings.xml configure path repository how:
<localRepository>E:\repositoryMavem</localRepository>
Hability your Eclipse (or IDE in use) for this file settings.xml
After go to path your project has pom.xml
run de command
mvn install:install-file -Dfile=E:\repositoryMavem\XMLManagement-1.0.jar -DgroupId=com.company -DartifactId=XMLManagement -Dversion=1.0 -Dpackaging=jar
if you want to use IDE Eclipse (you can).
if run clean for artifact, you need run install again

How do I build my Maven pom in Eclipse?

I have a project that uses UI4J, and instead of using external jar I decided to go for maven, I am going to distribute it via git, so I guessed that this is a much better approach.
This is 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>Kalamaria</groupId>
<artifactId>KalamariaHarverst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenFirst</name>
<dependencies>
<dependency>
<groupId>com.ui4j</groupId>
<artifactId>ui4j</artifactId>
<version>2.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
The problem is that I can't find a way to get this to work. What ever I try I am still getting errors on the import of the library, meaning that the jar of the ui4j is not imported.
I have (among others) tried to do a "Maven bumvn eclipse:eclipseild" with "clean install" as goal
Downloading source and Updating Project from the Maven menu
even tried to do a mvn eclipse:eclipse from the console, but i got this error
The program 'mvn' can be found in the following packages: * maven *
maven2 Try: sudo apt-get install
How does this work, what should I do to import the declared jars?
Removing <type></type> was the correct first step that you need to do.
Now, there's probably still a pom.lastUpdated file in your repository that is wrong, you need to forcibly override it. The easiest thing to do is just delete the entire directory in your .m2 directory, which is located in your OS dependent home directory. On windows, this would be:
C:\Users\<username>\.m2\repository\com\ui4j
On Linux, this is usually in:
/home/<username>/.m2/repository/com/ui4j
Delete that directory, and then do Maven -> Update project, this should fix your problem.
By the way, mvn eclipse:eclipse is almost never the right thing to do, it's much better to use m2eclipse for your eclipse integrations as it works much more seamlessly.
I am not able to reproduce the behavior of the pom type being added automatically when you add the ui4j dependency. However, most of the time the correct dependency <type> is jar, as that is the default. pom dependencies are most often used when a project is simply a pom and nothing else, which is common as the parent pom of an entire application.
In this case (as in most cases), the type you want is jar, so don't specify a type parameter.
To add a bit more background to #durron597 's correct answer:
tag defines what Maven looks for in the repository when it downloads your artifact. Various types exist. Most common and default is "jar" - Maven will look for something packaged as "jar" - a regular *.jar file. Other types include "test-jar", "war" and "pom". Type "pom" means that Maven will look for something packaged as "pom" - basically your dependency's pom.xml file. Most of the artifacts you refer to are packaged as "jar" and do not supply "pom" packaging. See http://maven.apache.org/pom.html#Dependencies, search for "Type".

Maven install not respecting command line arguments

I have a project which I am attempting to install with maven. The pom.xml has a few properties in it which are modified when the maven install command is run depending on whatever version of a library we are attempting to build with:
<properties>
<some-version>0</some-version>
</properties>
The zero here is a placeholder, as we'll always specify a legitimate version during our build process. The version is then referenced later in the pom.xml to specify a few dependencies:
<dependencies>
<dependency>
<groupId>com.mycompany.myproduct</groupId>
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
</dependencies
Building is done via make with the following commandline:
mvn -Dsome-version=1.6.2
Maven is able to correctly resolve the version and build as expected. However, the version being installed in my local maven repository (/home/user/.m2) doesn't have the correct version. The pom.xml that is installed does not have the updated version I set in the command line:
user#ubuntu:~/$ cat /home/user/.m2/repository/com/mycompany/myproduct/myproject/1.0.0/myproject-1.0.0.pom | grep some-version -C 1
<properties>
<some-version>0</some-version>
</properties>
--
<artifactId>someOtherProject</artifactId>
<version>${some-version}</version>
</dependency>
user#ubuntu:~/$
This is preventing any other project which depends on myproject from being able to build, as maven will complain that it can't find version 0 of someOtherProject:
[ERROR] Failed to execute goal on project myproject:
Could not resolve dependencies for project mycompany.myproduct:myproject:jar:1.0.0:
The following artifacts could not be resolved: com.mycompany.myproduct:someOtherProject:jar:0,
Could not find artifact com.mycompany.myproduct:someOtherProject:jar:0 in central (https://mycompany.com/artifactory/repo/) -> [Help 1]
What do I need to do for maven to install with the updated version in the pom? Obviously a terrible hackish solution would be to use sed and modify the pom file directly, but it seems that Maven should be able to actually leverage the command line settings when installing the pom. Otherwise the ability to set arguments on the command line seems remarkably limited in effectiveness.
Better you may set your property in pom.xml in <properties> tag like this -
<properties>
<property>
<name>some-version</name>
<value>1.6.2</value>
</property>
</properties>
If you use this then you don't have to provide the property each time you issue a mvn command from terminal.
mvn -Dsome-version=1.6.2 works as a substitution value for the scope of building than replacing the original POM with the new values. Hence is the behavior you see. I am not aware of any maven support to do so.
Under #JoopEggen's advice, I looked deeper into the maven versions plugin. It offered an update-property target which will actually update the pom.xml value on disk, rather than just passing in an overwrite during the build phase. I was able to solve my issue by calling
mvn versions:update-property -Dproperty=some-version -DnewVersion=1.6.2 -DsearchReactor=false -DallowSnapshots=true
in the makefile before calling mvn install. Disabling the reactor was necessary to prevent the plugin from rejecting values it couldn't find in the remote repo (see here), and allowSnapshots allows me to use version numbers such as 1.6.2-SNAPSHOT, useful when testing.

Categories

Resources