2017-09-16 112 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