maven 将本地jar包打入jar包中

有关MAVEN仓库的理解参见:http://blog.csdn.net/wanghantong/article/details/36427433

MAVEN依赖关系中Scope的作用

Java代码   收藏代码
  1. Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前依赖项的作用域<scope>可以使用5个值:    
  2. 在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。  
  3. compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。  
  4. runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。  
  5. test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。  
  6. provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。  
  7. system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。  

 

Java代码   收藏代码
  1. POM文件里面可以引用一些内置属性(Maven预定义可以直接使用)  
  2.   
  3. ${basedir} 项目根目录   
  4. ${version}表示项目版本;  
  5. ${project.basedir}同${basedir};  
  6. ${project.version}表示项目版本,与${version}相同;  
  7. ${project.build.directory} 构建目录,缺省为target  
  8. ${project.build.sourceEncoding}表示主源码的编码格式;  
  9. ${project.build.sourceDirectory}表示主源码路径;  
  10. ${project.build.finalName}表示输出文件名称;  
  11. ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes  

 

如何在Maven项目中引入本地包呢?

比如我从其它项目打一个jar包,引入到现有项目中。

方法一:将待引入的包放在目录下如lib目录下,修改pom文件,加入依赖并且scope要设置为system

 

Java代码   收藏代码
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>com.fbcds</groupId>  
  4.         <artifactId>fbcds</artifactId>  
  5.         <version>1.0</version>  
  6.         <scope>system</scope>  
  7.         <systemPath>${project.basedir}/lib/fbcds.jar</systemPath>  
  8.     </dependency>  
  9. </dependencies>  

 


 上面设置完成后,运行mvn package命令执行成功。但打出来的包里面不包含lib目录和fbcds.jar这个引用的包,即打出来的包不是可执行的jar。所以个人开发的话可以使用这种方式,如果团队开发请使用方法二。

 

方法二:将待引入的jar包安装到本地repository中

1、先把待引入的jar包放在一个目录下,需要改一下包名,如fbcds.jar修改成fbcds-1.0.jar,如F:\lib目录,在命令行CD到lib目录,执行以下命令:

Java代码   收藏代码
  1. mvn install:install-file -Dfile=fbcds-1.0.jar -DgroupId=fbcds -DartifactId=fbcds -Dversion=1.0 -Dpackaging=jar  
  2. mvn install:install-file -Dfile=ojdbc7-1.0.jar -DgroupId=ojdbc7 -DartifactId=ojdbc7 -Dversion=1.0 -Dpackaging=jar  

 2、修改项目pom文件加入包对应的依赖

 

Java代码   收藏代码
  1. <dependencies>  
  2.     <dependency>  
  3.       <groupId>log4j</groupId>  
  4.       <artifactId>log4j</artifactId>  
  5.       <version>1.2.17</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.       <groupId>fbcds</groupId>  
  9.       <artifactId>fbcds</artifactId>  
  10.       <version>1.0</version>  
  11.     </dependency>  
  12.     <dependency>   
  13.       <groupId>ojdbc7</groupId>  
  14.       <artifactId>ojdbc7</artifactId>  
  15.       <version>1.0</version>   
  16.     </dependency>  
  17.   </dependencies>  

 上面的fbcds和ojdbc7就是新加的引用包的依赖。

 完成后,在本地仓库可看到对应的文件夹内容:



 

MAVEN如何打可执行的JAR包

前提条件:已成功将待引入的jar包安装到本地repository中

方法一、使用maven-shade-plugin插件打可执行的jar包

插件查找链接:http://maven.apache.org/plugins/

1、测试类代码

 

Java代码   收藏代码
  1. package com.lwf.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. import com.eclink.fbcis.store.StoreDao;  
  10.   
  11. public class TestClass {  
  12.     public static void main(String[] args) {  
  13.         StoreDao a = new StoreDao();  
  14.         System.out.println("------" + a.toString());  
  15.         Connection con = null;  
  16.         Statement st = null;  
  17.         ResultSet rs = null;  
  18.         try {  
  19.             String sql = "select * from temp_head where temp_no='C530015I19008015'";  
  20.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  21.             con = DriverManager.getConnection("jdbc:oracle:thin:@//10.101.2.19:1521/pdbqmytcis","qmytcis","qmytcis123");  
  22.             st = con.createStatement();  
  23.             rs = st.executeQuery(sql);  
  24.             if(rs.next()){  
  25.                 System.out.println(rs.getString("temp_no"));  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally{  
  31.             try {  
  32.                 rs.close();  
  33.                 st.close();  
  34.                 con.close();  
  35.             } catch (SQLException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40. }  

 上面类中引用到了fbcds和ojdbc7包的内容。

2、对应pom文件

Java代码   收藏代码
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>222</groupId>  
  4.   <artifactId>222</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>222</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26.             <plugin>  
  27.                 <groupId>org.apache.maven.plugins</groupId>  
  28.                 <artifactId>maven-shade-plugin</artifactId>  
  29.                 <version>2.4.3</version>  
  30.                 <executions>  
  31.                     <execution>  
  32.                         <phase>package</phase>  
  33.                         <goals>  
  34.                             <goal>shade</goal>  
  35.                         </goals>  
  36.                         <configuration>  
  37.                             <transformers>  
  38.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  39.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  40.                                 </transformer>  
  41.                             </transformers>  
  42.                         </configuration>  
  43.                     </execution>  
  44.                 </executions>  
  45.             </plugin>  
  46.         </plugins>  
  47.     </build>  
  48.   
  49. </project>  

 在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:



 比较两个包内容:



 执行包:cmd下



 original-MavenPackage-0.0.1-SNAPSHOT.jar中没有主清单属性是执行不了的。

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/

方法二、使用maven-assembly-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码   收藏代码
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 使用 maven-Assembly-plugin插件打可执行包-->  
  51.             <plugin>  
  52.                 <groupId>org.apache.maven.plugins</groupId>  
  53.                 <artifactId>maven-assembly-plugin</artifactId>  
  54.                 <version>2.6</version>  
  55.                 <configuration>  
  56.                     <!-- get all project dependencies -->  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <!-- MainClass in mainfest make a executable jar -->  
  61.                     <archive>  
  62.                       <manifest>  
  63.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  64.                       </manifest>  
  65.                     </archive>  
  66.                 </configuration>  
  67.                 <executions>  
  68.                   <execution>  
  69.                     <id>make-assembly</id>  
  70.                     <phase>package</phase>   
  71.                     <goals>  
  72.                         <goal>single</goal>  
  73.                     </goals>  
  74.                   </execution>  
  75.                 </executions>  
  76.             </plugin>  
  77.         </plugins>  
  78.     </build>  
  79.   
  80. </project>  

 修改完pom后,在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:



 两个jar文件比较:



 



 

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

方法三、使用onejar-maven-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码   收藏代码
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 使用 maven-Assembly-plugin插件打可执行包-->  
  51. <!--   
  52.             <plugin>  
  53.                 <groupId>org.apache.maven.plugins</groupId>  
  54.                 <artifactId>maven-assembly-plugin</artifactId>  
  55.                 <version>2.6</version>  
  56.                 <configuration>  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <archive>  
  61.                       <manifest>  
  62.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  63.                       </manifest>  
  64.                     </archive>  
  65.                 </configuration>  
  66.                 <executions>  
  67.                   <execution>  
  68.                     <id>make-assembly</id>  
  69.                     <phase>package</phase>   
  70.                     <goals>  
  71.                         <goal>single</goal>  
  72.                     </goals>  
  73.                   </execution>  
  74.                 </executions>  
  75.             </plugin>  
  76. -->                   
  77.             <!-- 使用 onejar-maven-plugin插件打可执行包-->  
  78.             <plugin>  
  79.                 <groupId>org.apache.maven.plugins</groupId>  
  80.                 <artifactId>maven-jar-plugin</artifactId>  
  81.                 <configuration>  
  82.                     <archive>  
  83.                         <manifest>  
  84.                             <mainClass>com.lwf.test.TestClass</mainClass>  
  85.                         </manifest>  
  86.                     </archive>  
  87.                 </configuration>  
  88.             </plugin>  
  89.             <plugin>  
  90.                 <groupId>com.jolira</groupId>  
  91.                 <artifactId>onejar-maven-plugin</artifactId>  
  92.                 <version>1.4.4</version>  
  93.                 <executions>  
  94.                     <execution>  
  95.                         <configuration>  
  96.                             <attachToBuild>true</attachToBuild>  
  97.                             <classifier>onejar</classifier>  
  98.                         </configuration>  
  99.                         <goals>  
  100.                             <goal>one-jar</goal>  
  101.                         </goals>  
  102.                     </execution>  
  103.                 </executions>  
  104.             </plugin>  
  105.         </plugins>  
  106.     </build>  
  107. </project>  

打包截图如下:




 

 
 

参见:http://www.mkyong.com/maven/maven-create-a-fat-jar-file-one-jar-example/

上文中因googlecode中已没有onejar-maven-plugin所以另请参见下文:

http://my.oschina.net/noahxiao/blog/78241

方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下

其他不变,pom文件如下

Java代码   收藏代码
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 方法一:使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 方法二:使用 maven-Assembly-plugin插件打可执行包-->  
  51. <!--   
  52.             <plugin>  
  53.                 <groupId>org.apache.maven.plugins</groupId>  
  54.                 <artifactId>maven-assembly-plugin</artifactId>  
  55.                 <version>2.6</version>  
  56.                 <configuration>  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <archive>  
  61.                       <manifest>  
  62.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  63.                       </manifest>  
  64.                     </archive>  
  65.                 </configuration>  
  66.                 <executions>  
  67.                   <execution>  
  68.                     <id>make-assembly</id>  
  69.                     <phase>package</phase>   
  70.                     <goals>  
  71.                         <goal>single</goal>  
  72.                     </goals>  
  73.                   </execution>  
  74.                 </executions>  
  75.             </plugin>  
  76. -->        
  77.              
  78. <!-- 方法三:使用 onejar-maven-plugin插件打可执行包-->  
  79. <!--           
  80.             <plugin>  
  81.                 <groupId>org.apache.maven.plugins</groupId>  
  82.                 <artifactId>maven-jar-plugin</artifactId>  
  83.                 <configuration>  
  84.                     <archive>  
  85.                         <manifest>  
  86.                             <mainClass>com.lwf.test.TestClass</mainClass>  
  87.                         </manifest>  
  88.                     </archive>  
  89.                 </configuration>  
  90.             </plugin>  
  91.             <plugin>  
  92.                 <groupId>com.jolira</groupId>  
  93.                 <artifactId>onejar-maven-plugin</artifactId>  
  94.                 <version>1.4.4</version>  
  95.                 <executions>  
  96.                     <execution>  
  97.                         <configuration>  
  98.                             <attachToBuild>true</attachToBuild>  
  99.                             <classifier>onejar</classifier>  
  100.                         </configuration>  
  101.                         <goals>  
  102.                             <goal>one-jar</goal>  
  103.                         </goals>  
  104.                     </execution>  
  105.                 </executions>  
  106.             </plugin>  
  107.               
  108.      -->          
  109.           
  110. <!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下 -->  
  111.             <plugin>  
  112.                 <groupId>org.apache.maven.plugins</groupId>  
  113.                 <artifactId>maven-jar-plugin</artifactId>  
  114.                 <configuration>  
  115.                   <excludes>  
  116.                     <exclude>**/log4j.properties</exclude>  
  117.                   </excludes>  
  118.                   <archive>  
  119.                     <manifest>  
  120.                     <addClasspath>true</addClasspath>  
  121.                     <mainClass>com.lwf.test.TestClass</mainClass>  
  122.                     <classpathPrefix>lib/</classpathPrefix>  
  123.                     </manifest>  
  124.                   </archive>  
  125.                 </configuration>  
  126.             </plugin>  
  127.   
  128.             <!-- Copy project dependency -->  
  129.             <plugin>  
  130.                 <groupId>org.apache.maven.plugins</groupId>  
  131.                 <artifactId>maven-dependency-plugin</artifactId>  
  132.                 <version>2.5.1</version>  
  133.                 <executions>  
  134.                   <execution>  
  135.                     <id>copy-dependencies</id>  
  136.                     <phase>package</phase>  
  137.                     <goals>  
  138.                         <goal>copy-dependencies</goal>  
  139.                     </goals>  
  140.                     <configuration>  
  141.                       <!-- exclude junit, we need runtime dependency only -->  
  142.                       <includeScope>runtime</includeScope>  
  143.                       <outputDirectory>${project.build.directory}/lib/</outputDirectory>  
  144.                     </configuration>  
  145.                   </execution>  
  146.                 </executions>  
  147.             </plugin>              
  148.         </plugins>  
  149.     </build>  
  150. </project>  

 



可以看到依赖的包拷贝到了lib目录下,打的包里没有依赖包的信息,只是简单的包,不过Manifest文件class-path要包含引用名的路径

Java代码   收藏代码
  1. Manifest-Version: 1.0  
  2. Built-By: lweifeng  
  3. Build-Jdk: 1.7.0_17  
  4. Class-Path: lib/log4j-1.2.17.jar lib/fbcds-1.0.jar lib/ojdbc7-1.0.jar  
  5. Created-By: Apache Maven 3.3.9  
  6. Main-Class: com.lwf.test.TestClass  
  7. Archiver-Version: Plexus Archiver  

 
 在以上前三种插件打包方式中,maven-shade-plugin和maven-assembly-plugin采取的是将依赖包解压再一并打到新包中,这样依赖包可能存在冲突的时候,导致运行时可能出现未知问题,而onejar-maven-plugin打包是将依赖包自动归入lib目录,不解压原包,相当于在原包基础上加壳,这样可以避免冲突的发生。第四种方法即是我们原来ant打包所使用的方法。


  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Maven是一种基于项目对象模型的构建工具,它可以管理项目的构建、依赖和文档。在Maven,我们可以使用Maven插件将本地包打入jar包。打包时需要在pom.xml文件配置插件管理。 首先,我们需要将本地的jar包安装到本地Maven仓库。在命令行使用Maven的install命令完成安装,例如: ``` mvn install:install-file -Dfile=path/to/local.jar -DgroupId=com.example -DartifactId=my-local-jar -Dversion=1.0 -Dpackaging=jar ``` 参数解释: - -Dfile: 需要安装的本地jar文件 - -DgroupId: 组织ID,通常是公司名称或组织名称,用来唯一标识一个项目组 - -DartifactId: 项目ID,用来标识具体的项目,例如my-local-jar - -Dversion: 版本号,用来标识不同版本的相同项目 - -Dpackaging: 打包方式,这里是以jar包的形式打包 安装完成后,我们可以在本地Maven仓库找到刚刚安装的jar包。 接下来,在pom.xml文件配置打包插件。常用的插件有maven-jar-plugin和maven-assembly-plugin。其maven-jar-plugin适用于简单的jar包打包,而maven-assembly-plugin可以将依赖的jar包一起打包。 在pom.xml文件的build节点下添加插件管理: ```xml <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.2</version> <configuration> <archive> <manifest> <mainClass>com.example.Main</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> ``` 配置说明: - groupId、artifactId和version定义打包插件的依赖,这里选择了maven-jar-plugin插件的版本3.2.2 - configuration节点下的archive节点定义了打包的配置,这里添加了一个manifest节点,用于设置可执行的Main类 完成配置后,我们可以使用Maven的package命令进行打包,例如: ``` mvn package ``` 打包完成后,我们可以在target目录下找到生成的jar包。该jar包包含了本地打入jar包以及其他依赖的jar包。使用java -jar命令即可运行该jar包。 总结起来,本地包打入jar包的过程包括安装本地jar包Maven仓库、配置打包插件和执行打包命令。通过Maven统一管理依赖和打包,可以简化开发过程,方便维护项目。 ### 回答2: Maven是一套管理Java项目的工具,通常用于自动化构建、依赖管理、测试和部署等方面。在某些情况下,我们需要将Maven本地的jar包打入到新的jar包,以便于在其他项目使用。 首先,我们需要在本地的Maven仓库找到需要打入jar包的依赖,可以通过Maven的命令来查看本地仓库的依赖: ```bash mvn dependency:tree -DoutputFile=dependencies.txt ``` 该命令会输出当前项目的依赖树,并将依赖信息保存到一个名为dependencies.txt的文件。在文件,可以找到需要打入jar包的依赖的坐标信息。 接下来,在项目的pom.xml添加以下插件配置: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>jar-with-dependencies</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <finalName>new-jar</finalName> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.xxx.App</mainClass> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> <includes> <include>com/xxx/**</include> </includes> <outputDirectory>${basedir}</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 该插件将在打包时将依赖也打入到生成的jar包,并将依赖包放在lib目录下。其,finalName属性指定了生成的jar包的名称,mainClass属性指定了主类,includes属性指定了需要打包的类所在的包名。 最后,在命令行执行以下命令进行打包: ```bash mvn package ``` 该命令将会生成一个包含依赖的新的jar包,可以在target目录下找到。打包完成后,可以将该jar包拷贝到其他项目使用。 总结来说,Maven本地包打入jar包需要通过查找本地仓库的依赖,配置maven-jar-plugin插件并执行打包命令来完成。该方法适用于一些特殊需求,如需要将多个依赖打入一个jar包,或者需要将一些不能通过Maven仓库来获取的依赖打入jar包等等。 ### 回答3: Maven本地包打入jar包是让我们在使用Maven构建Java项目时将本地的Java工程打包成jar包的过程。本地包是指我们自己编写的Java代码或者将别人提供的Java代码放到本地仓库的包,以便在自己的项目引用。 对于Maven来说,打包成jar包是为了便于管理和使用,可以方便的加入到依赖库进行使用,从而避免了重复的代码,提高了代码的复用性。 下面是Maven本地包打入jar包的步骤: 1. 编写Java代码,将其打包方式设置为jar。 2. 在pom.xml文件添加本地引用: ``` <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0-SNAPSHOT</version> <scope>system</scope> <systemPath>${basedir}/lib/example.jar</systemPath> </dependency> ``` 其,groupId,artifactId和version都是自己定义的,本地jar包的路径需要根据实际情况来进行设置。 3. 运行Maven命令,将本地库jar包打入到项目: ``` mvn install:install-file -DgroupId=com.example -DartifactId=example -Dversion=1.0-SNAPSHOT -Dfile=lib/example.jar -Dpackaging=jar ``` 其,groupId,artifactId,version和本地jar包的路径需要根据实际情况来进行设置。 4. 运行Maven命令,生成项目的jar包: ``` mvn clean package ``` 执行该命令后,生成的jar包将包含本地的Java代码和引用的本地jar包。 总的来说,Maven本地包打入jar包是一个比较简单的过程,只需要按照上述步骤进行操作即可。对于使用Maven的Java项目而言,引入本地包可以让代码更加简单、方便,也可以提高代码的复用性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值