2012-09-04

用同一個 pom.xml 產出多種 jar 檔

Maven 的原則是「一個 pom.xml 只會產出一種 jar 檔」,這是基於「模組化」的關係,如果要產出多種 jar 檔,就要拆成多個 pom.xml。

雖然如此,但還是很大方的提供了產出多種 jar 檔的方法

  <profiles>
    <profile>
      <id>impl</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <classifier>impl</classifier>
              <excludes>
                <exclude>**/mock</exclude>
                <exclude>**/mock/*</exclude>
                <exclude>**/proxy</exclude>
                <exclude>**/proxy/*</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>mock</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <classifier>mock</classifier>
              <excludes>
                <exclude>**/impl</exclude>
                <exclude>**/impl/*</exclude>
                <exclude>**/proxy</exclude>
                <exclude>**/proxy/*</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>proxy</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <classifier>proxy</classifier>
              <excludes>
                <exclude>**/mock</exclude>
                <exclude>**/mock/*</exclude>
                <exclude>**/impl</exclude>
                <exclude>**/impl/*</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
然後執行不同參數的 mvn package 就可以得到多種 jar 檔。
mvn package -Pimpl
mvn package -Pmock
mvn package -Pproxy
---

沒有留言:

張貼留言