2017-09-16 70 views
2

我在解决这个问题时遇到了一些麻烦,所以我会尽我所能去尽可能多地抽象出不相关的细节。如果需要更多细节,请询问。在maven中设置属性标志时如何跳过下载依赖关系

我有一个包含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

+1

一个依赖或者是可选的,就像你给的链接中描述的那样,或者你需要它......你能举个例子吗? – khmarbaise

回答

2

你可以做到这一点使用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,则将仅从主依赖块中将jmespath-java重新加载。

希望这会有所帮助。

+0

正是我在找什么,谢谢! – barthelonafan

+0

我忘了问,我还有一个build下的步骤,解压缩或解压缩content.zip文件,如果'-Dcontent = false'构建步骤仍然执行,我怎么也只有当'-Dcontent = TRUE'? – barthelonafan

+0

看起来像是通过将''元素与'' – barthelonafan

相关问题