软件编程
位置:首页>> 软件编程>> java编程>> Java中Maven项目导出jar包配置的示例代码

Java中Maven项目导出jar包配置的示例代码

作者:Strugglion  发布时间:2023-01-26 20:09:32 

标签:maven,jar包

具体代码如下所示;


<!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 -->
<build>
 <sourceDirectory>src/main/java</sourceDirectory>
 <resources>
   <resource>
     <directory>src/main/resources</directory>
     <!-- 将<directory>目录下的文件打包到<targetPath>下 -->
     <targetPath>${project.build.directory}</targetPath>
     <excludes>
       <exclude>**/*.java</exclude>
     </excludes>
   </resource>
 </resources>
 <testSourceDirectory>src/test/java</testSourceDirectory>
 <testResources>
   <testResource>
     <directory>src/test/resources</directory>
     <filtering>true</filtering>
     <excludes>
       <exclude>**/*.java</exclude>
     </excludes>
   </testResource>
 </testResources>
 <plugins>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
       <source>1.8</source>
       <target>1.8</target>
       <encoding>UTF-8</encoding>
     </configuration>
   </plugin>
   <!-- 将项目依赖包复制到<outputDirectory>指定的目录下 -->
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
       <execution>
         <id>copy-dependencies</id>
         <phase>package</phase>
         <goals>
           <goal>copy-dependencies</goal>
         </goals>
         <configuration>
           <outputDirectory>
             ${project.build.directory}/lib
           </outputDirectory>
         </configuration>
       </execution>
     </executions>
   </plugin>
   <!-- 将项目依赖包的路径(与上一项中的<outputDirectory>对应)添加到classPath中 -->
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <configuration>
       <archive>
         <manifest>
           <addClasspath>true</addClasspath>
           <classpathPrefix>lib/</classpathPrefix>
           <mainClass>com.ctbri.echoRisk.ApplicationStart</mainClass>
         </manifest>
         <manifestEntries>
           <Class-Path>./</Class-Path>
         </manifestEntries>
       </archive>
     </configuration>
   </plugin>      
 </plugins>
</build>

<!-- 第二种打包方式 (maven-shade-plugin), 将依赖包和配置文件放到jar包内 -->
<build>    
 <directory>${project.basedir}/target</directory>
 <finalName>${project.artifactId}-${project.version}</finalName>
 <plugins>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.21.0</version>
     <configuration>
       <skipTests>true</skipTests>
     </configuration>
   </plugin>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
       <source>1.8</source>
       <target>1.8</target>
       <encoding>UTF-8</encoding>
       <compilerArguments>
         <extdirs>${project.basedir}/lib/</extdirs>
       </compilerArguments>
     </configuration>
   </plugin>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>3.1.0</version>
     <executions>
       <execution>
         <phase>package</phase>
         <goals>
           <goal>shade</goal>
         </goals>
         <configuration>
           <minimizeJar>true</minimizeJar>
           <filters>
             <filter>
               <artifact>*:*</artifact>
             </filter>
           </filters>
           <transformers>
             <!-- 往MANIFEST文件中写入Main-Class是可执行包的必要条件。ManifestResourceTransformer可以轻松实现。 -->
             <transformer
               implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
               <mainClass>com.ctbri.echoRisk.ApplicationStart</mainClass>
             </transformer>
             <!-- AppendingTransformer 用来处理多个jar包中存在重名的配置文件的合并,尤其是spring -->
             <transformer
               implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
               <resource>META-INF/spring.handlers</resource>
             </transformer>
             <transformer
               implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
               <resource>META-INF/spring.schemas</resource>
             </transformer>
           </transformers>
         </configuration>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

<!-- 第三种打包方式 (maven-assembly-plugin) -->
<build>
 <plugins>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.21.0</version>
     <configuration>
       <skipTests>true</skipTests>
     </configuration>
   </plugin>
   <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>2.3.2</version>
     <configuration>
       <source>1.8</source>
       <target>1.8</target>
       <encoding>UTF-8</encoding>
       <compilerArguments>
         <extdirs>${project.basedir}/lib/</extdirs>
       </compilerArguments>
     </configuration>
   </plugin>
   <plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <configuration>
       <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <archive>
         <manifest>
           <mainClass>com.ctbri.echoRisk.ApplicationStart</mainClass>
         </manifest>
       </archive>
     </configuration>
     <executions>
       <execution>
         <id>make-assembly</id>
         <phase>package</phase>
         <goals>
           <goal>single</goal>
         </goals>
       </execution>
     </executions>
   </plugin>
 </plugins>
</build>

总结

以上所述是小编给大家介绍的Java中Maven项目导出jar包配置的示例代码网站的支持!

来源:https://www.cnblogs.com/strugglion/archive/2018/11/26/10020218.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com