How to execute maven goal but ignore potential local pom.xml? - java

I have a script that shall use maven to download an artifact to the local maven repository:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:get -Dartifact=org.jasypt:jasypt:1.9.3
However, when the script is executed from a directory that contains a pom.xml then maven also parses this POM and prints the reactor, etc. causing confusion. Even worse when the pom.xml is invalid for whatever reason the command fails.
What I want is to ignore any local pom.xml. Is there a way to do this with a maven CLI option or something similar?
I already studied the CLI options but could not find any option for this. I tried with something like -f /tmp but it seems that -f option does the oposite of what I want as it requires a POM file or a directory containing a pom.xml or the build will immediately fail.
Any other ideas instead of creating an empty directory in my script, CDing to it, invoking maven and then CD back and delete the empty directory?

My work-around is to have a minimal POM reflecting an artifact created by the Maven invocation that needs to ignore the actual POM,
groupId=".."
artId=".."
ver=".."
echo "<project>
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artId}</artifactId>
<version>${ver}</version>
</project>" > target/min-pom.xml
mvn -f target/min-pom.xml PLUGIN_GOAL_CREATING_GROUP_ARTIFACT_VER
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#minimal-pom

Related

when i add apache deryby dependecy it gives error [duplicate]

I imported my already working project on another computer and it started to download dependencies.
Apparently my internet connection crashed and now I get the following:
>Build errors for comics; org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal on project comicsTest: Could not resolve dependencies for project comicsTest:comicsTest:war:0.0.1-SNAPSHOT:
The following artifacts could not be resolved:
org.springframework:spring-context:jar:3.0.5.RELEASE,
org.hibernate:hibernate-entitymanager:jar:3.6.0.Final,
org.hibernate:hibernate-core:jar:3.6.0.Final,
org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final,
org.aspectj:aspectjweaver:jar:1.6.8,
commons-lang:commons-lang:jar:2.5,
>mysql:mysql-connector-java:jar:5.1.13: Failure to transfer org.springframework:spring-context:jar:3.0.5.RELEASE from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced.
>Original error: Could not transfer artifact org.springframework:spring-context:jar:3.0.5.RELEASE from central (http://repo1.maven.org/maven2): No response received after 60000
How do I force maven to update?
mvn clean install -U
-U means force update of snapshot dependencies.
Release dependencies will be updated this way if they have never been previously successfully downloaded. ref: https://stackoverflow.com/a/29020990/32453
If your local repository is somehow mucked up for release jars as opposed to snapshots (-U and --update-snapshots only update snapshots), you can purge the local repo using the following:
mvn dependency:purge-local-repository
You probably then want to clean and install again:
mvn dependency:purge-local-repository clean install
Lots more info available at https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html
-U seems to force update of all SNAPSHOT dependencies.
If you want to update a single dependency without clean or -U you could just remove it from your local repo and then build.
The example below if for updating slf4j-api 1.7.1-SNAPSHOT:
rm -rf ~/.m2/repository/org/slf4j/slf4j-api/1.7.1-SNAPSHOT
mvn compile
All the answers here didn't work for me. I used the hammer method:
find ~/.m2/ -name "*.lastUpdated" | xargs rm
That fixed the problem :-)
You can do effectively from Eclipse IDE. Of course if you are using it.
Project_Name->Maven->Update Project Configuration->Force Update of Snapshots/Releases
Just in case someone wants only update project's snapshot dependencies and doesn't want to install artifact:
mvn dependency:resolve -U
Don't forget to reimport dependencies in your IDE. In IDEA you need to right click on pom file and choose Maven -> Reimport
If you're unsure what is inside your local repository, I recommend to fire a build with the option:
-Dmaven.repo.local=localrepo
That way you'll ensure to build in a cleanroom environment.
In my case first I did was:
mvn clean install -U
Still it was showing same error then I closed project and again reopened it. Finally worked.
If you are using eclipse IDE then :
Select Project.
Press alt+F5, window for Update Maven Project will pop up.
Check - Force Update of Snapshots/releases and click OK.
If Using Intellij IDE
go to settings/Maven
check Always update snapshots
I used the IntelliJ IDE and I had a similar problem and to solve I clicked in "Generate Sources and Update Folders for All Projects" in Maven tab.
Previous versions of maven did not force the check for missing releases when used -U with mvn clean install, only the snapshots, though newer version supports this.
For someone still struggling with previous version, following can be helpful-
On Windows:
cd %userprofile%\.m2\repository
for /r %i in (*.lastUpdated) do del %i
On Linux:
find ~/.m2 -name "*.lastUpdated" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \;
Whenever maven can't download dependencies for any reason (connectivity/not exists etc), it will add the ".error=Could not transfer artifact" in dependency-name.lastUpdate file in respective folder under $home/.m2 directory. Removing these files will force maven to try fetching the dependencies again.
mvn clean install -e -U -Dmaven.test.skip=true
-e Detailed exception
-U forced update
-DskipTests does not execute test cases, but compiles test case classes to generate corresponding class files under target/test classes.
-Dmaven.test.skip=true, do not execute test cases or compile test case classes.Using maven. test. skip not only skips running unit tests, but also skips compiling test code.
A small suggestion. If you use the IntelliJ Idea compiler, it is recommended to clean the cache
I've got the error in an other context.
So my solution might be useful to others who stumple upon the question:
The problem:
I've copied the local repository to another computer, which has no connection to a special repository.
So maven tried to check the artifacts against the invalid repository.
My solution:
Remove the _maven.repositories files.
You need to check your settings.xml file under <maven_home>/conf directory.
This is one of the most annoying things about Maven. For me the following happens: If I add a dependency requesting more dependencies and more and more but have a slow connection, it seams to stop while downloading and timing out. While timing out all dependencies not yet fetched are marked with place holders in the .m2 cache and Maven will not (never) pick it up unless I remove the place holder entry from the cache (as other stated) by removing it.
So as far as I see it, Maven or more precise the Eclipse Maven plugin has a bug regarding this. Someone should report this.
It's important to add that the main difference of running mvn with -U and without -U is that -U will override your local SNAPSHOT jars with remote SNAPSHOT jars.
Local SNAPSHOT jars created from local mvn install in cases where you have other modules of your proj that generate jars.
For fixing this issue from Eclipse:
1) Add below dependency in Maven pom.xml and save the pom.xml file.
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
2) Go to project >> Maven >> Update Project
select the project and click OK.
3) Optional step, if it's not resolved till step 2 then do below step after doing step-1
Go to project >> Maven >> Update Project >> check in the checkbox 'Force Update of Snapshots/Releases'
select the project and click OK.
-U is used to force update maven Repo.
Use
mvn -U clean install
I've got the same error with android-maps-utils dependency. Using aar type package in dependency section solve my problem.
By default type is jar so It might be checked what type of dependency in repository is downloaded.
I tried all the answers here but nothing seemed to work. Restarted my computer first then ran mvn clean install -U. That solved my problem.
What maven does is, it downloads all your project's dependencies into your local repo (.m2 folder). Because of the internet causing issues with your local repo, you project is facing problems. I am not sure if this will surely help you or not but you can try deleting all the files within the repository folder inside the .m2 folder. Since there would be nothing in the local repo, maven would be forced to download the dependencies again, thus forcing an update.
Generally, the .m2 folder is located at c:users:[username]:.m2
after using mvn clean install -U run as maven test also and after that update your project using maven-update project
this works in my case
I had this problem for a different reason. I went to the maven repository https://mvnrepository.com looking for the latest version of spring core, which at the time was 5.0.0.M3/ The repository showed me this entry for my pom.xml:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.M3</version>
</dependency>
Naive fool that I am, I assumed that the comment was telling me that the jar is located in the default repository.
However, after a lot of head-banging, I saw a note just below the xml saying "Note: this artifact it located at Alfresco Public repository (https://artifacts.alfresco.com/nexus/content/repositories/public/)"
So the comment in the XML is completely misleading. The jar is located in another archive, which was why Maven couldn't find it!
We can force to get latest update of release and snapshot repository with below command :
mvn --update-snapshots clean install
I had the same error and running mvn install -U and then running mvn install worked for me.
mvn clean install -U doesn't work. However mvn -U clean followed by mvn clean install does.

Run maven with specific path to certain plugin from command line

I would need to execute a specific Maven plugin from command line. For example, in the following I execute a specific version of Maven Surefire Plugin to test Java projects:
mvn org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
However, the above assumes to find the surefire plugin 2.19 in the default Maven repository path. Now, my question is, if I want to use the plugin with a specific path (not Maven default one), what should I do using the command line? I would expect something like the following, without modifying pom.xml:
mvn /path/to/some/jar/version/org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
or more generally, for the following invocation
mvn groupId:artifactId:version:goal
I would need somewhere to specify a customized path to execute its goal
mvn /some/path/to/groupId:artifactId:version:goal
On the other hand, please let me know if this is not even supported by Maven.
This is not how it works. Maven will always look-up artifacts inside your local repository. And if it can't find it in your local repository, it will try to download it from configured remote repositories.
As such, you don't specify a path to a plugin. You specify a path to a local repository, where that plugin is installed. In this local repository will be installed all the dependencies of the plugin you're trying to invoke. This also means that you cannot have a JAR to a plugin "sitting around" anywhere; it needs to be located inside a correct repository tree directory. With a local repository of /my/local/repo, the artifacts of the plugin groupId:artifactId:version must be located in /my/local/repo/groupId/artifactId/version and be named artifactId-version.jar (and .pom). In the same say, the location of the dependencies of that plugin must follow that directory structure.
By default, the local repository is located inside ~/.m2/repository. But you can change that by:
Specifying the maven.repo.local system property on the command line, for example with mvn -Dmaven.repo.local=/path/to/local/repo groupId:artifactId:version:goal;
Use a custom settings.xml and tell Maven to use it with the -s command line option. It would contain:
<settings>
<localRepository>/path/to/local/repo</localRepository>
</settings>
and be used with mvn -s /path/to/settings.xml groupId:artifactId:version:goal

couldn't create maven project [duplicate]

I imported my already working project on another computer and it started to download dependencies.
Apparently my internet connection crashed and now I get the following:
>Build errors for comics; org.apache.maven.lifecycle.LifecycleExecutionException:
Failed to execute goal on project comicsTest: Could not resolve dependencies for project comicsTest:comicsTest:war:0.0.1-SNAPSHOT:
The following artifacts could not be resolved:
org.springframework:spring-context:jar:3.0.5.RELEASE,
org.hibernate:hibernate-entitymanager:jar:3.6.0.Final,
org.hibernate:hibernate-core:jar:3.6.0.Final,
org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final,
org.aspectj:aspectjweaver:jar:1.6.8,
commons-lang:commons-lang:jar:2.5,
>mysql:mysql-connector-java:jar:5.1.13: Failure to transfer org.springframework:spring-context:jar:3.0.5.RELEASE from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced.
>Original error: Could not transfer artifact org.springframework:spring-context:jar:3.0.5.RELEASE from central (http://repo1.maven.org/maven2): No response received after 60000
How do I force maven to update?
mvn clean install -U
-U means force update of snapshot dependencies.
Release dependencies will be updated this way if they have never been previously successfully downloaded. ref: https://stackoverflow.com/a/29020990/32453
If your local repository is somehow mucked up for release jars as opposed to snapshots (-U and --update-snapshots only update snapshots), you can purge the local repo using the following:
mvn dependency:purge-local-repository
You probably then want to clean and install again:
mvn dependency:purge-local-repository clean install
Lots more info available at https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html
-U seems to force update of all SNAPSHOT dependencies.
If you want to update a single dependency without clean or -U you could just remove it from your local repo and then build.
The example below if for updating slf4j-api 1.7.1-SNAPSHOT:
rm -rf ~/.m2/repository/org/slf4j/slf4j-api/1.7.1-SNAPSHOT
mvn compile
All the answers here didn't work for me. I used the hammer method:
find ~/.m2/ -name "*.lastUpdated" | xargs rm
That fixed the problem :-)
You can do effectively from Eclipse IDE. Of course if you are using it.
Project_Name->Maven->Update Project Configuration->Force Update of Snapshots/Releases
Just in case someone wants only update project's snapshot dependencies and doesn't want to install artifact:
mvn dependency:resolve -U
Don't forget to reimport dependencies in your IDE. In IDEA you need to right click on pom file and choose Maven -> Reimport
If you're unsure what is inside your local repository, I recommend to fire a build with the option:
-Dmaven.repo.local=localrepo
That way you'll ensure to build in a cleanroom environment.
In my case first I did was:
mvn clean install -U
Still it was showing same error then I closed project and again reopened it. Finally worked.
If you are using eclipse IDE then :
Select Project.
Press alt+F5, window for Update Maven Project will pop up.
Check - Force Update of Snapshots/releases and click OK.
If Using Intellij IDE
go to settings/Maven
check Always update snapshots
I used the IntelliJ IDE and I had a similar problem and to solve I clicked in "Generate Sources and Update Folders for All Projects" in Maven tab.
Previous versions of maven did not force the check for missing releases when used -U with mvn clean install, only the snapshots, though newer version supports this.
For someone still struggling with previous version, following can be helpful-
On Windows:
cd %userprofile%\.m2\repository
for /r %i in (*.lastUpdated) do del %i
On Linux:
find ~/.m2 -name "*.lastUpdated" -exec grep -q "Could not transfer" {} \; -print -exec rm {} \;
Whenever maven can't download dependencies for any reason (connectivity/not exists etc), it will add the ".error=Could not transfer artifact" in dependency-name.lastUpdate file in respective folder under $home/.m2 directory. Removing these files will force maven to try fetching the dependencies again.
mvn clean install -e -U -Dmaven.test.skip=true
-e Detailed exception
-U forced update
-DskipTests does not execute test cases, but compiles test case classes to generate corresponding class files under target/test classes.
-Dmaven.test.skip=true, do not execute test cases or compile test case classes.Using maven. test. skip not only skips running unit tests, but also skips compiling test code.
A small suggestion. If you use the IntelliJ Idea compiler, it is recommended to clean the cache
I've got the error in an other context.
So my solution might be useful to others who stumple upon the question:
The problem:
I've copied the local repository to another computer, which has no connection to a special repository.
So maven tried to check the artifacts against the invalid repository.
My solution:
Remove the _maven.repositories files.
You need to check your settings.xml file under <maven_home>/conf directory.
This is one of the most annoying things about Maven. For me the following happens: If I add a dependency requesting more dependencies and more and more but have a slow connection, it seams to stop while downloading and timing out. While timing out all dependencies not yet fetched are marked with place holders in the .m2 cache and Maven will not (never) pick it up unless I remove the place holder entry from the cache (as other stated) by removing it.
So as far as I see it, Maven or more precise the Eclipse Maven plugin has a bug regarding this. Someone should report this.
It's important to add that the main difference of running mvn with -U and without -U is that -U will override your local SNAPSHOT jars with remote SNAPSHOT jars.
Local SNAPSHOT jars created from local mvn install in cases where you have other modules of your proj that generate jars.
For fixing this issue from Eclipse:
1) Add below dependency in Maven pom.xml and save the pom.xml file.
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
2) Go to project >> Maven >> Update Project
select the project and click OK.
3) Optional step, if it's not resolved till step 2 then do below step after doing step-1
Go to project >> Maven >> Update Project >> check in the checkbox 'Force Update of Snapshots/Releases'
select the project and click OK.
-U is used to force update maven Repo.
Use
mvn -U clean install
I've got the same error with android-maps-utils dependency. Using aar type package in dependency section solve my problem.
By default type is jar so It might be checked what type of dependency in repository is downloaded.
I tried all the answers here but nothing seemed to work. Restarted my computer first then ran mvn clean install -U. That solved my problem.
What maven does is, it downloads all your project's dependencies into your local repo (.m2 folder). Because of the internet causing issues with your local repo, you project is facing problems. I am not sure if this will surely help you or not but you can try deleting all the files within the repository folder inside the .m2 folder. Since there would be nothing in the local repo, maven would be forced to download the dependencies again, thus forcing an update.
Generally, the .m2 folder is located at c:users:[username]:.m2
after using mvn clean install -U run as maven test also and after that update your project using maven-update project
this works in my case
I had this problem for a different reason. I went to the maven repository https://mvnrepository.com looking for the latest version of spring core, which at the time was 5.0.0.M3/ The repository showed me this entry for my pom.xml:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.0.M3</version>
</dependency>
Naive fool that I am, I assumed that the comment was telling me that the jar is located in the default repository.
However, after a lot of head-banging, I saw a note just below the xml saying "Note: this artifact it located at Alfresco Public repository (https://artifacts.alfresco.com/nexus/content/repositories/public/)"
So the comment in the XML is completely misleading. The jar is located in another archive, which was why Maven couldn't find it!
We can force to get latest update of release and snapshot repository with below command :
mvn --update-snapshots clean install
I had the same error and running mvn install -U and then running mvn install worked for me.
mvn clean install -U doesn't work. However mvn -U clean followed by mvn clean install does.

Using Maven Archetype Generate in the same Directory

Is there a way to run mvn archetype:generate and target the current directory instead of creating a directory from the artifactId? The plugin supposedly takes a basedir parameter, but passing in "-Dbasedir=." doesn't do the trick.
For additional context, I've got 2 Git repositories setup. The first contains the source of a Maven archetype for generating custom web services, and the second is a sample service generated from the archetype. I've got a Jenkins job that builds my archetype by essentially just running "mvn clean install". I'm trying to setup a second Jenkins job as part of our CI workflow generates a test service using "mvn archetype:generate", builds the service with mvn clean install, spins up the service, runs some integration tests, and then pushes the source for the test service into a second repository if the tests pass. Both Jenkins jobs use the Maven 2/3 build job type, and I've specified our Git repo information in the SCM section of the job configuration, so the jobs start by doing a "git clone".
For the second job, my current workflow looks like this:
// Clean out the existing source and commit locally.
git rm -r .
git commit -m "Cleaning out previous version." .
// Generate the new source from the archetype.
mvn archetype:generate ...
// Big hack that I'd like to remove.
mv <artifactId>/* .
rm -rf <artifactId>
// Add the new generated source and commit locally.
git add -A .
git commit -m "Committing new version." .
// Build and test.
mvn integration-test
// Assuming that passed, commit the changes.
git push origin master
The hack is there because I'm not sure how to tell the archetype plugin to use my current directory instead of creating a nested directory with the name of the artifactId, so I have to move everything back into my local repository root directory and delete the directory that was created. Not the end of the world, but it feels ugly. Note that I'm open to suggestions on how to better accomplish my overall goal in addition to answers to my initial question. :) Thanks in advance!
It is not possible without some kind of workaround, because of 2 reasons:
The basedir property doesn't work, see this issue.
The archetype plugin always adds artifactId as the last child directory. You can see this in the sources.
Your options
keep your hack, it is not so horrible ;-)
is it important that the project is in the root of the repository? if not just do cd <artifactId> before the build/integration tests
if building from root of the repository is required then use root pom.xml of type pom, with the generated project as child module (I think this is what other post suggests as #3)
provide a PR to add desired functionality to maven archetype plugin, this might take some time, but is probably the cleanest solution
I know it's old topic but I had similar issue:
The archetype:generate goal has a parameter outputDirectory, you can set it to root folder (../). When generating artifact from archetype it's important that your artifact name is same as current directory.
mvn archetype:generate -DarchetypeGroupId=[archetype-group] -DarchetypeArtifactId=[archetype-id] -DoutputDirectory=../ -DartifactId=[current-folder-name] -DgroupId=[whatever-you-want] -Dversion=[ex: 0.1] -Dpackage=[jar|pom|etc.] -B
I have three ideas:
You can try to use an absolute path for basedir but I think the property is overwritten by Maven when it sees a pom.xml file.
Use a script and pushd .. to change to the parent folder before you run mvn archetype:generate. popd will get you back.
Use a maven module build. That way, you have a root POM which builds the archetype and you can run mvn archetype:generate to generate a module inside of the project:
demo-project/
pom.xml
archetype/
pom.xml
I don't have the reputation to comment so I'll have to make an answer instead.
It's not a good idea to have these lines in your script:
mv <artifactId>/* .
rm -rf <artifactId>
If someone decides to fill in an empty (or just '/') artifactId you end up trying to move the whole disk into this directory, only to then delete the entire disk. Instead you can do this:
mv ./<artifactId>/* .
rmdir ./<artifactId>
The move can't hurt and the rmdir can only delete empty directories.

Maven command to download latest version of a jar

We have a process to update part of a build that is not in Maven yet but will be soon. Once built, we manually put the jar in Nexus and then update the pom files that are in SVN. Works fine and doing a "mvn clean install" pulls down the new .jar and creates a new folder in my local repository etc. Grand.
Now I would like to be able to get any new version that has been uploaded by making a mvn call without doing a build (as with mvn install). I was looking at mvn dependency:get but it seems to have a lot of parameters that only gave me errors.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get(default-cli) on project workflow-project: The parameters 'repositoryUrl' for goal org.apache.maven.plugins:maven-dependency-plugin:2.1:get are missing or invalid -> [Help 1]
I have tried adding the mentioned repositoryUrl param but it too gives me errors, not least about deprecation. Is this the correct goal? How is it most simply used if so?
Use the Maven Versions plugin. It has a ton of useful commands for managing dependency versions in a POM.
For example, you can run the following:
mvn versions:use-latest-versions
That command will replace versions in your POM with the latest version of artifacts. There are optional parameters you can pass to have more fine-grained control. For example, you can use the includes parameter to specify a particular artifact to update to the latest version.
Perhaps you would be happy with a
mvn compile
Which would load the dependencies and compile but go no futher. The only changes would be in target

Categories