如何在 Maven 中设置 属性 标志时跳过下载依赖项

How to skip downloading a dependency when a property flag is set in maven

我在表达这个问题时遇到了一些问题,所以我会尽力抽象掉尽可能多的无关细节。如果需要更多详细信息,请询问。

我有一个包含 pom 的项目,该 pom 包含一个依赖项,当用户对该 pom 执行 mvn clean install 时,该依赖项总是被下载并解压缩到目录中。但是,当用户传入 属性 例如 mvn clean install -Dcontent=false 但在该 pom.xml 中执行其他所有操作时,我想放弃下载和解压缩该依赖项。

由于缺乏更好的表达方式,我想知道如何在 maven 中使某个依赖项成为可选的?在此处描述的意义上不是可选的: http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

但如上所述在构建时是可选的。

编辑:

构建步骤

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.company.random</groupId>
                                <artifactId>content</artifactId>
                                <version>${contentVersion}</version>
                                <type>zip</type>
                                <outputDirectory>contentdir/target</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    <plugins>
</build>

@Mikita 目前这将一直执行,我怎么能让它只在 -Dcontent=true

时执行

您可以使用 Maven 配置文件来做到这一点。在示例中有一个 content 配置文件,如果 属性 content 将在 true 中设置,该配置文件将被激活。并且仅在这种情况下下载 poi 依赖项,否则不要下载。

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.Whosebug</groupId>
    <artifactId>profile-question</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>War application with optional dependencies</name>

    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>jmespath-java</artifactId>
            <version>1.11.197</version>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>content</id>
            <activation>
                <property>
                    <name>content</name>
                    <value>true</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                    <version>3.7</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

同样,仅当您使用以下命令时才会下载 poi:mvn clean install -Dcontent=true。如果您不指定 content 参数或在 false 中设置它,将仅从主依赖块中下载 jmespath-java

希望这会有所帮助。