Maven解压缩最新版本的tar.gz工件

nef*_*o_x 13 maven-3 maven

目标是tar.gz从存储库中获取最新的工件并将其解压缩到某个特定位置.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.5.1</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.enterprise</groupId>
                                <artifactId>skrillex</artifactId>
                                <version>${product.version}</version>
                                <type>tar.gz</type>
                                <outputDirectory>target/product</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

还有

<dependencies>
    <dependency>
        <groupId>com.enterprise</groupId>
        <artifactId>skrillex</artifactId>
        <version>${product.version}</version>
        <type>tar.gz</type>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

但我们得到错误:

[INFO] --- maven-dependency-plugin:2.5.1:unpack (unpack-unix) @ ... ---
[INFO] Configured Artifact: com.enterprise:skrillex:[1.1.70,):tar.gz
Downloading: https://repo/com/enterprise/skrillex/[1.1.70,)/skrillex-[1.1.70,).tar.gz

...

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.5.1:unpack (unpack-unix) on project ...: Unable to resolve artifact. Could not transfer artifact com.enterprise:skrillex:tar.gz:[1.1.70,) from/to ext (repo....): IllegalArgumentException
Run Code Online (Sandbox Code Playgroud)

Ste*_*lly 13

注意:以下是未经测试的,但应该可以使用

好的,这里的问题是<artifactItem>无法解析版本范围.

你需要做的是切换dependency:unpackdependency:unpack-dependencies

例如

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
            <execution>
                <phase>generate-resources</phase>
                <goals>
                    <goal>unpack-dependencies</goal>
                </goals>
                <configuration>
                    <includeTypes>tar.gz</includeTypes>
                    <includeArtifactIds>skrillex</includeArtifactIds>
                    <outputDirectory>target/product</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

(对于其他任何人,您需要添加依赖项,确保指定类型,例如

<dependencies>
    <dependency>
        <groupId>com.enterprise</groupId>
        <artifactId>skrillex</artifactId>
        <version>${product.version}</version>
        <type>tar.gz</type>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

)

这应该确保Maven解析范围,并且由于文件类型不是类路径兼容的,因此依赖关系不会在此工件的传递类路径上.

如果你用的classpath兼容的依赖关系,例如这样.jar的依赖,或可视为这种依赖性,例如.zip在依赖.war,那么你将要添加两种<scope>test</scope><optional>true</optional>到依赖,这样的传递依赖关系树不被污染.

潜在问题

您需要注意一些事项:

  • Maven 2.x不会跟踪远程存储库中的副工件存在,因此如果.tar.gz没有附加到每个版本(或者更严重地说每个-SNAPSHOT版本),那么最终可能会找不到工件

  • Maven 3.x确实跟踪了侧面工件的存在maven-metadata.xml但IIRC只针对-SNAPSHOT版本,这个想法是,如果你部署"部分"快照,你仍然可以解决所有最新的副本工件(即使最新版本是针对-SNAPSHOT相同版本的版

  • 使用版本范围是一个非常糟糕的计划.随着范围根据上游的更新设置得到解决,它将给项目的下游消费者带来痛苦的世界<repositories>.请重新考虑并使用固定版本.

  • 仅供参考,戴着我的Apache Maven开发者帽子.如果以上版本不适用于版本范围,那么您发现了一个错误.不要混淆成为bug的可能性与Maven开发人员认为你_should_使用版本范围.我们考虑的意见是版本范围是一个诱人的警笛,我们希望我们没有听过. (2认同)

小智 4

如果您不介意两步过程,请使用以下 pom:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.enterprise</groupId>
<artifactId>skrillex-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
    <skrillex.version>[0.0.0,1.0.0)</skrillex.version>
</properties>
<packaging>jar</packaging>
<build>
  <plugins>
    <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>versions-maven-plugin</artifactId>
     <version>2.0</version>
     <configuration>
            <includes>
                    <include>com.enterprise:*</include>
            </includes>
     </configuration>
   </plugin>
   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
            <execution>
                <phase>generate-resources</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>com.enterprise</groupId>
                            <artifactId>skrillex</artifactId>
                            <version>${skrillex.version}</version>
                            <type>jar</type>
                            <outputDirectory>target/product</outputDirectory>
                        </artifactItem>
                    </artifactItems>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.enterprise</groupId>
        <artifactId>skrillex</artifactId>
        <version>${skrillex.version}</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

首先运行:(mvn versions:resolve-ranges它会使用属性中所需的版本更新您的 pom)

接下来是你想要的 Maven 目标,例如:mvn install

现在如果你想恢复原来的pom:mvn versions:revert