2015-10-20 82 views
1

我有一個使用單個POM的Maven項目。我有多個配置文件和兩個配置文件特定屬性文件。使用Maven創建多個配置文件jar

我想創建多個jar文件,每個配置文件id一個。對於這個可加載的jar,我需要添加配置文件特定的屬性文件。 罐子不應該擁有所有的依賴關係,只有所有的類文件,以便每個罐子可以作爲其他Maven項目中的依賴關係。

問題:

  1. 有沒有人這樣做過,這可以在Maven的做或做我需要使用antrun建設者?
  2. 如果可以做什麼是Maven插件的最佳組合使用?
  3. 我正在生成其名稱具有配置文件ID擴展名的屬性文件,在將它們添加到jar之前,必須將這些名稱更改爲默認名稱。
  4. 似乎很容易做一個或另一個....只是jar或屬性文件...但生成特定的jar與特定的屬性文件是最大的痛苦。

任何示例POM XML將不勝感激。

+0

您可以使用多個插件執行此操作,如[本答案](http://stackoverflow.com/a/12321395/944849)中所述。 – user944849

+0

您需要具有不同的依賴關係,您需要將其關閉。而配置文件是錯誤的方式。 – khmarbaise

回答

1

那麼,一種做法是通過Maven程序集。

結構:

multi-profile 
| 
+-- pom.xml 
+-- profile-1.properties 
+-- profile-2.properties 
+-- src 
    | 
    +-- assembly 
    | | 
    | +-- profile-1.xml 
    | +-- profile-2.xml 
    | 
    +-- main 
     | 
     +-- java 
      | 
      + ... 

POM:

<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>org.example</groupId> 
    <artifactId>multi-profile</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>multi-profile</name> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <build> 
    <pluginManagement> 
     <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
      <execution> 
       <id>create-assembly</id> 
       <phase>package</phase> 
       <goals> 
       <goal>single</goal> 
       </goals> 
      </execution> 
      </executions> 
     </plugin> 
     </plugins> 
    </pluginManagement> 
    </build> 
    <profiles> 
    <profile> 
     <id>profile-1</id> 
     <build> 
     <plugins> 
      <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptors> 
       <descriptor>src/assembly/profile-1.xml</descriptor> 
       </descriptors> 
       <appendAssemblyId>true</appendAssemblyId> 
      </configuration> 
      </plugin> 
     </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>profile-2</id> 
     <build> 
     <plugins> 
      <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptors> 
       <descriptor>src/assembly/profile-2.xml</descriptor> 
       </descriptors> 
       <appendAssemblyId>true</appendAssemblyId> 
      </configuration> 
      </plugin> 
     </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>profile-3</id> 
     <build> 
     <plugins> 
      <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptors> 
       <descriptor>src/assembly/profile-1.xml</descriptor> 
       <descriptor>src/assembly/profile-2.xml</descriptor> 
       </descriptors> 
       <appendAssemblyId>true</appendAssemblyId> 
      </configuration> 
      </plugin> 
     </plugins> 
     </build> 
    </profile> 

    </profiles> 
</project> 

一個組件(profile-1.xml,另一種是非常相似):

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <id>profile-1</id> 
    <formats> 
    <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <files> 
     <file> 
     <source>${project.basedir}/profile-1.properties</source> 
     <outputDirectory>/</outputDirectory> 
     <destName>config.properties</destName> 
     </file> 
    </files> 
    <dependencySets> 
     <dependencySet> 
     <!-- This will exclude any transitive dependencies from being included in your assembly --> 
     <includes> 
      <include>org.example:multi-profile</include> 
     </includes> 
     <useTransitiveDependencies>false</useTransitiveDependencies> 
     <unpack>true</unpack> 
     </dependencySet> 
    </dependencySets> 
</assembly> 

如果運行mvn clean install則沒有屬性文件將被包括在內。如果運行mvn -P profile-3 clean install將在一個單一版本中創建所有配置文件(在單獨的工件中)。程序集會根據需要重命名屬性文件(在本例中爲config.properties)。

如果一些其他的項目需要這些的一個「輪廓建立」作爲依賴,只提到這樣的:

<dependency> 
    <groupId>org.example</groupId> 
    <artifactId>multi-profile</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <classifier>profile-1</classifier> 
</dependency> 

希望它爲你工作。

+0

非常感謝@丹尼爾的魅力! –