开发者

Maven 2: Only build artifact if not already installed in local repository

开发者 https://www.devze.com 2023-04-03 18:33 出处:网络
How can I achieve in Maven that when executing \"mvn install\" from the command line will automatically decide to build the artifact (jar/war) only if that artifact has not already been installed in t

How can I achieve in Maven that when executing "mvn install" from the command line will automatically decide to build the artifact (jar/war) only if that artifact has not already been installed in the local repository?

I've already tried with profiles, the problem here is that in the "activation" task I cannot reference my local repository through a Maven property. I cannot hardcode the local repository as that would break my build in a different environment.

I also tried a workaround by touching a file in a previous phase, e.g. initialize and then check for the existence of the file as activation con开发者_StackOverflow社区dition, but this doesn't work because the activation condition is evaluated before the initializing is performed.

Any hints or ideas how to solve this problem?

Thanks, Peter


The short answer is: there's no sane way to do that, Maven doesn't work that way.

What you could do is create a shell script that tries to resolve the project from the local repo first (use dependency:get) and calls mvn clean install only if that first part fails. But the problem with that is that you'll have to use the project's exact groupId, artifactId and version in the call, as this won't work:

mvn dependency:get -DrepoUrl=${localRepository} -DgroupId=${project.groupId} \
    -DartifactId=${project.artifactId} -Dversion=${project.version} \
    -Dtransitive=false

Unfortunately the variables are expanded at a time when project is not yet available.


So the only good solution would be to do it programmatically. Either write a custom plugin or create a groovy script using GMaven. Try to resolve the project artifact from the repository and throw a MojoFailureException if you succeed. But it's still hacky and I'd just leave it.


If the maven-install-plugin would have a skip configuration this should be possible with a profile somehow like this (maybe you have to modify the system property to get your home directory with the m2 repository):

<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>foo.bar</groupId>
    <artifactId>lorem-ipsum</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <skip.install>false</skip.install>
    </properties>

    <profiles>
        <profile>
            <id>already-installed</id>
            <activation>
                <file>
                    <exists>${env.USERPROFILE}/.m2/repository/foo/bar/${project.artifactId}/${project.version}/${project.artifactId}-${project.version}.${project.packaging}</exists>
                </file>
            </activation>
            <properties>
                <skip.install>true</skip.install>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <skip>${skip.install}</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Unfortunately the maven-install-plugin does not have the skip configuration possibility. There is an improvement request MINSTALL-73 on this. Maybe you should upvote this request to get the patch included in a future maven release.

So, as Sean Patrick Floyd mentioned already there is no "built-in" way to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号