articles:maven_packaging

Mavenで標準以外の成果物(artifact)を作成する

mvn packagetarget/${project.artifactId}-${project.version}-jar-with-dependencies.jar を生成。

pom.xml
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>fat-jar</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.example.Main</mainClass>
                </manifest>
              </archive>
              <attach>false</attach>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

詳細は assembly:single を参照。

Mavenの理念は一つのモジュールに一つの成果物。 本来は別のモジュールにするべき。

executionを複数作成し、configurationをその中に移動する。 jarファイル名がぶつかるのでfinalNameで変更する。

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <id>make-first-jar</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>somepackage.FirstMainClass</mainClass>
              </manifest>
            </archive>
            <finalName>first</finalName>
          </configuration>
        </execution>
        <execution>
          <id>make-second-jar</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <archive>
              <manifest>
                <mainClass>somepackage.SecondMainClass</mainClass>
              </manifest>
            </archive>
            <finalName>second</finalName>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

mvn packagetarget/${project.artifactId}-${project.version}-executable.jar を生成。

pom.xml
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>executable-jar</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/assembly/executable.xml</descriptor>
              </descriptors>
              <archive>
                <manifest>
                  <mainClass>com.example.Main</mainClass>
                </manifest>
              </archive>
              <attach>false</attach>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
src/assembly/executable.xml
<assembly
  xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
                      http://maven.apache.org/xsd/assembly-2.0.0.xsd">
 
  <id>executable</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <scope>runtime</scope>
      <useProjectArtifact>true</useProjectArtifact>
      <outputDirectory></outputDirectory>
      <unpack>true</unpack>
      <excludes>
        <!-- use groupId:artifactId[:type[:classifier][:version]] -->
        <!-- 通常typeは'jar' でclassifierはなし -->
        <exclude>log4j:log4j</exclude>
        <exclude>org.slf4j:slf4j-log4j12:jar:1.7.10</exclude>
      </excludes>
    </dependencySet>
  </dependencySets>
</assembly>

mvn packagetarget/lib に依存するjarファイルをコピーする。

pom.xml
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <includeScope>runtime</includeScope>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

mvn packagetarget/${project.artifactId}-${project.version}-sources.jar を生成。

pom.xml
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

mvn packagetarget/${project.artifactId}-${project.version}-javadoc.jar を生成。

pom.xml
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-javadoc-plugin</artifactId>
        <executions>
          <execution>
            <id>attach-javadocs</id>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

mvn packagetarget/${project.artifactId}-${project.version}-bin.ziptarget/${project.artifactId}-${project.version}-bin.tar.gz を生成。

pom.xml
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>create-archive</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/assembly/bindist.xml</descriptor>
              </descriptors>
              <attach>false</attach>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
src/assembly/bindist.xml
<assembly
  xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
                      http://maven.apache.org/xsd/assembly-2.0.0.xsd">
 
  <id>bin</id>
  <formats>
    <format>tar.gz</format>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>src/dist</directory>
      <outputDirectory></outputDirectory>
      <excludes>
        <exclude>bin/*.sh</exclude>
      </excludes>
    </fileSet>
    <fileSet>
      <directory>src/dist</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>bin/*.sh</include>
      </includes>
      <fileMode>0755</fileMode>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>lib</outputDirectory>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
</assembly>

アセンブリデスクリプタの詳細は Assembly を参照.

  • 最終更新: 2020-04-21 13:18
  • by nabium