gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:25:27 25 4
gpt4 key购买 nike

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

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

由于缺乏更好的表达方式,我想知道如何在 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.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,则只会从主依赖 block 下载 jmespath-java

希望这会有所帮助。

关于xml - 如何在 Maven 中设置属性标志时跳过下载依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46256925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com