【问题标题】:How to skip downloading a dependency when a property flag is set in maven在 Maven 中设置属性标志时如何跳过下载依赖项
【发布时间】:2018-02-25 16:05:25
【问题描述】:

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

我有一个包含 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时才执行

【问题讨论】:

  • 依赖项是可选的,如您提供的链接中所述的方式,或者您需要它...您可以举个例子吗?

标签: xml maven pom.xml


【解决方案1】:

您可以使用 Maven 配置文件来做到这一点。例如,如果属性content 将在true 中设置,则会激活content 配置文件。并且仅在这种情况下下载poidependecy,否则不要。

<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.stackoverflow</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

希望这会有所帮助。

【讨论】:

  • 我忘了问,我还有一个构建步骤,它解压或解压缩 content.zip 文件,如果 -Dcontent=false 构建步骤仍然执行,我怎么能只在-Dcontent=true?
  • 看起来只要用&lt;build&gt; 包裹&lt;profile&gt; 就可以了
  • @barthelonafan 当然。 Profile 几乎可以包含所有元素……甚至是modules。但想法保持不变......实现取决于您计划解决的具体问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2012-08-23
  • 2015-01-16
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
相关资源
最近更新 更多