can maven clean delete deleted dependency? - java

I have many dependencies in pom.xml. As I go through my project, I installed dependencies which are not used anymore(due to trial and error).
So I deleted dependencies which are not used anymore in pom.xml.
Performed mvn clean then mvn install.
Does the dependencies which I deleted in pom.xml no longer exist in maven repository?

mvn clean do not clean your repository. It only cleans the project.

You can do it manually, as commented by #Arnaud, or you might prefer an automated way using the maven-dependency-plugin with the purge-local-repository goal on a project which declares your deleted dependencies only:
mvn dependency:purge-local-repository

With the help of Purging local repository dependencies you need to do that. But this is little bit tricky. So be careful while you are using it.
I think dependency:purge-local-repository and Optional Parameter excludes will help you.
excludes nothing but : The list of dependencies in the form of groupId:artifactId which should NOT be deleted/refreshed.
For full documentation see this.

Related

How to decide which plugin to use in maven?

I was going through the maven documentation. I saw maven works using plugins, for each phase there is one or more plugin and for each plugin there is one or more goals. there are plugin for compile, install, deploy, clean etc. I saw in my project they used maven-clean-plugin. Then how maven clean install command is working if we did not put the maven-install-plugin? Question might be bit silly but i am new the this world and appreciate your help.
That would follow up with some default configuration of each plugin inherited with Maven's BOM. Executing
mvn dependency:resolve-plugins
shall help you identify these versions of the plugins inherited.
So primarily to ensure you work with a specific(or updated) version of the plugin with the desired configuration of your project, you can explicitly define them in the pom.xml, otherwise, just let be.
The Maven lifecycle has standard bindings for plugins to the different phases.
If you run mvn clean install you run the plugins that a attached to the phases by default.

Remove unnecessary dependencies in POM

I am new to Maven and I faced a problem when I tried to convert my current project from using Ant -> using Maven.
That project requires many Jars, and I look up those jars on mvnrepository and add all to POM.xml.
I don't know if some dependency is redundant.
Do you guy know any way to check if which dependency which I really need?
The Maven dependency analyzer plugin is just what you're looking for.
Just run
mvn install dependency:analyze
(on some platforms, for some reason, the full notation is required:)
mvn install org.apache.maven.plugins:maven-dependency-plugin:2.9:analyze
And review the report it produces.

Add artifact to local repository from project

Is there a way add artifact to local maven repository from my eclipse project?
currently i have a project that contain many jars, and i have started using maven. what i need is to add all these jars to the local repository in an automated way without redownload them or adding them one by one and specifying their coordinates.
Make a new Maven project in Eclipse, and add all your code to the src/main directory. Now you will have lots of compile errors, because of missing dependencies.
Now start auto adding the dependencies. In Intellj you can add something using alt-enter, which also has the option to "add maven dependency". This adds that dependency from the maven repository to the pom. I do not know eclipse well enough, but it probably also has this feature.
Now, in a normal project, you will find most of your required dependencies somewhere in Maven Central. If you miss any, you can add them using manual installation to your local repository, as suggested by Manas Mukherjee
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar
you can write a script using mvn install command.
mvn install:install-file -Dfile={jar_file_name_path}.jar -DgroupId={groupId}
-DartifactId={artifactId} -Dversion={version} -Dpackaging=jar
You can add all dependencies in pom file as well
Thanks

Download dependencies to local repository but not to "target" one

I have a project, that has pom.xml and depends on lots of dependencies from outside (located far-far in internet..).
So, I want to download all those dependencies which I depend on to my "local repository".
This is my try (I do not need do compilation, so I use "validate" here. So I'm do not expect to have "target" folder in the end):
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:copy-dependencies
In the end - yes I have many dependencies been downloaded to "C:\my\.m2\repository", but some of them went to: C:\projects\myProject\java\trunk\target\dependency, like these ones:
junit-4.8.1.jar
log4j-1.2.8.jar
mockito-all-1.8.2.jar
Question is: how to make those to be downloaded to "C:\my\.m2\repository" but not to "target" of my project?
For now, because of that, another projects that depend on that are failing while building, because they are expecting to find "junit-4.8.1.jar" in local repo.
Another try:
mvn validate -Dmaven.repo.local=C:\my\.m2\repository dependency:resolve
Then those dependencies are not resolvable at all.
Could not resolve dependencies for project bla-bla-SNAPSHOT: The
following artifacts could not be resolved:
commons-lang:commons-lang:jar:2.4, log4j:log4j:jar:1.2.8,
junit:junit:jar:4.8.1: Could not find artifact
commons-lang:commons-lang:jar:2.4 -> [Help 1]
Maven did that because you invoked the goal dependency:copy-dependencies. It will copy the dependencies of the current module to
${project.build.directory}/dependency
See http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
I really can't imagine these dependencies did not get into your local repo. Fiddling around with a script may not be the solution to your problem. Try
mvn process-resources -U -Dmaven.repo.local=C:\my\.m2\repository
The -U option forces a download of all dependencies. I suggest using process-resources, although as of my understanding validate should be fine, too.

Maven does not know my internal project

I have two projects A and B managed with maven and B depends on A. (Additionally they both have external dependencies from the public repositories).
When I run mvn compile on A everything is ok.
When I run mvn compile on B it tells me 1 required artifact is missing.
Doing mvn install on A does not help. What should I do?
I should add that these are two different projects rather than two modules of 1 project. Help.
UPDATE
It was just a typo in the referencing pom.xml, which I discovered thanks to #Raghuram's comment
Doing mvn install should be enough - check there are no typos in your pom.xml files.
Either install it manually to your local repository or use a repository manager like Nexus.
I would, as Rohan mentions as a comment to your question, create a parent pom you can execute the install goal for, including your two "internal" projects as modules.

Categories