Java – Maven 3 does not update snapshot dependencies from the local repository

I'm trying to use external libraries in my Maven project Since I want the project out of the box on any machine, I don't want to use MVN installation solution So I'm in my POM The local repository is defined in XML:

<dependency>
        <groupId>com.test</groupId>
        <artifactId>fooLib</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    ....
    <repository>
        <id>in-project</id>
        <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
        </snapshots>
        <name>In Project Repo</name>
        <url>file://${project.basedir}/libRepo</url>
    </repository>

The problem is that when I replace the jar in librepo (I don't update the version number because it's just another snapshot), I don't use the updated jar (I use the old version of the. M2 directory), even for MVN - u clean install, how can Maven update the jar?

Edit: according to what exactly is a maven snapshot and why do we need it? Maven will try to find a snapshot dependent version that will never be released, "even if the version of this library is found in the local repository." What's wrong with my settings?

Dirty solution: according to Maven 2 assembly with dependencies: jar under scope "system" not included, the extension of my original solution seems to work:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <id>hack-binary</id>
                    <phase>validate</phase>
                    <configuration>
                        <file>${repo.path.to.jar}</file>
                        <repositoryLayout>default</repositoryLayout>
                        <groupId>com.test</groupId>
                        <artifactId>fooLib</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                    </configuration>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

As mentioned in the comments on the solution, it cannot work alone, so it works with the project memory repository (when dependencies are not available in the local. M2 repository), and the second part is refreshed during each build m2.

However, I still don't know why the ordinary "snapshot" mechanism doesn't work (that is, the current dirty solution can also work without snapshot, because the local. M2 repo is explicitly updated every time) Is there a cleaner way?

Solution (based on Aaron's answer and discussion): the problem is that I try to install the file into librepo using install file The actual solution is, if the library is updated, use

mvn deploy:deploy-file -Dfile=fooLib.jar  -DgroupId=com.test \
    -DartifactId=fooLib -Dversion=1.0-SNAPSHOT -Dpackaging=jar \
    -Durl=file://..\libRepo -DrepositoryId=in-project

Deploy it to repo After the correct deployment, Maven correctly handles snapshot

Solution

If you use a repository, Maven copies the jars to its local repository (usually in $home /. M2 / repository /) Maven will not consider this file changed or copy it unless the version number is changed Please note that the version number is the only thing Maven sees; It doesn't care about checksum or file size or date

By the way, a version number is assigned internally for this purpose: Maven can internally notice that the snapshot has been updated

I suggest using system dependency instead In this way, the actual jar will be added to the classpath (without any replication or anything) You also don't need to copy the repo structure of this method, it will clearly convey your intention

[editor] I know Maven handles dependencies on the scope system in different ways I'm not sure if this makes sense (if it compiles with dependencies, it can certainly run with it?)

In my opinion, you have these options:

>Use deploy: deploy file to install the dependency into librepo instead of copying it yourself This should update the metadata in such a way that when you run MVN install again on a real project, Maven will copy it again

Note that file: install does not work The file plug-in is used to access the local repository, but you need to use the deploy plug-in that knows how to update the shared / server repository. > Install dependencies into the local repository; I suggest you use a script that you can include in your project In this way, you can avoid all problems and easily set up on your new machine. > Changing the version number of a dependency is tedious, and you may have trouble when the actual version number of a dependency changes. > Set up a local warehouse server for your company and deploy dependencies to that company This will take a few hours, but a) you will get a local cache of all dependencies to make your initial build faster, and b) it will be set up faster for other developers

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>