5

I'm using Maven for my project. It is not an issue if my project doesn't use some local resources. So that I'm following this guide https://stackoverflow.com/a/61576687/6720896 to copy my local jar to local maven repository and validated by Maven.

As you can see, in the maven-install-plugin, I'm setting to install local repository at clean phase. It means by mvn clean, maven will copy my jar to maven local repository.

The problem is, if I run mvn clean and mvn install by two commands separately => there is no problem If i run mvn clean install => the build is failed as the log

Caused by: org.apache.maven.project.DependencyResolutionException: Could not resolve dependencies for project xxxx:xxxx:war:0.0.1-SNAPSHOT: Failure to find org.xxxx:xxxx-ws:jar:1.0.0 in https://repo.maven.apache.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

Seem like by default, maven always execute following order: validate > compile > clean > install. I also tried with 'mvn clean validate' but the error still occurs.

Thank you for reading.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • 1
    What do you mean by `..copy my local jar to local maven repository and validated by maven`? – khmarbaise Aug 28 '20 at 18:12
  • To directly answer your question title — `mvn clean install` simply means to run _clean_ and then run _install_. It _should_ give you the same result as if you ran `mvn clean` and then you separately ran `mvn install`. I think @Donat has it right that you are doing things in "clean" that you should not be doing. `clean` should _**not**_ "copy my jar to maven local repository" – Stephen P Aug 28 '20 at 19:08
  • 1
    @StephenP There is a subtle difference - see my answer. – J Fabian Meier Aug 28 '20 at 20:20
  • @JFabianMeier - excellent point, and I overlooked that because that difference has never mattered to me in the ways I use maven, but the difference _is_ there. – Stephen P Aug 28 '20 at 20:24

1 Answers1

5

The question is not really clear, but my interpretation is as follows:

The OP wants to automatically add a dependency to the local repository by using install:install-file. Then the OP wants to use that dependency. This works if mvn clean and mvn install are run separately, but not if one runs mvn clean install.

The reason is as follows:

Maven resolves dependencies at the beginning of the process. So dependencies are already resolved before the clean of mvn clean install is executed.

This especially implies that you cannot install and resolve a dependency in the same Maven run.

If, though, you first run mvn clean (which installs the dependency) and then mvn install (which uses the dependency), everything is fine.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Yes, you're right and so sorry for making it very confusion. This is the issue that i got. By separating `mvn clean install` to two commands `mvn clean` and `mvn install` => it is solved. But as you said, maven resolves dependencies at the beginning of the process, but for `mvn clean` it doesn't. – nguyen dang Thanh Aug 29 '20 at 17:56
  • "mvn clean" installs the dependency? Does not it delete existing .class files and dependencies are resolved during "mvn install"? – Muhammad Abu Bakr Oct 12 '22 at 11:16