1. 程式人生 > >Maven 打jar包配置

Maven 打jar包配置

1.修改pom.xml增加如下內容

  1. <plugin>
  2.     <groupId>org.apache.maven.plugins</groupId>
  3.     <artifactId>maven-jar-plugin</artifactId>
  4.     <version>2.4</version>
  5.     <configuration>
  6.         <archive>
  7.             <manifest>
  8.                 <
    addClasspath>true</addClasspath>
  9.                 <classpathPrefix>lib/</classpathPrefix>
  10.                 <mainClass>com.sysware.HelloWorld</mainClass>
  11.             </manifest>
  12.         </archive>
  13.     </configuration>
  14. </plugin>

執行mvn clean package即可

2.在pom.xml增加如下內容

  1. <plugin>
  2.     <groupId>org.apache.maven.plugins</groupId>
  3.     <artifactId>maven-assembly-plugin</artifactId>
  4.     <version>2.3</version>
  5.     <configuration>
  6.         <appendAssemblyId>false</appendAssemblyId>
  7.         <
    descriptorRefs>
  8.             <descriptorRef>jar-with-dependencies</descriptorRef>
  9.         </descriptorRefs>
  10.         <archive>
  11.             <manifest>
  12.                 <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>
  13.             </manifest>
  14.         </archive>
  15.     </configuration>
  16.     <executions>
  17.         <execution>
  18.             <id>make-assembly</id>
  19.             <phase>package</phase>
  20.             <goals>
  21.                 <goal>assembly</goal>
  22.             </goals>
  23.         </execution>
  24.     </executions>
  25. </plugin>
執行mvn assembly:assembly

3.

  1. <build>
  2.         <finalName>...</finalName>
  3.         <sourceDirectory>src/main/java</sourceDirectory>
  4.         <resources>
  5.             <!-- 控制資原始檔的拷貝 -->
  6.             <resource>
  7.                 <directory>src/main/resources</directory>
  8.                 <targetPath>${project.build.directory}</targetPath>
  9.             </resource>
  10.         </resources>
  11.         <plugins>
  12.             <!-- 設定原始檔編碼方式 -->
  13.             <plugin>
  14.                 <groupId>org.apache.maven.plugins</groupId>
  15.                 <artifactId>maven-compiler-plugin</artifactId>
  16.                 <configuration>
  17.                     <defaultLibBundleDir>lib</defaultLibBundleDir>
  18.                     <source>1.6</source>
  19.                     <target>1.6</target>
  20.                     <encoding>UTF-8</encoding>
  21.                 </configuration>
  22.             </plugin>
  23.             <!-- 打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->
  24.             <plugin>
  25.