欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用maven将项目打成jar包

程序员文章站 2024-02-07 23:06:34
...
               

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent>  <groupId>wusc.edu.common</groupId>  <artifactId>edu-common-parent</artifactId>  <version>1.0-SNAPSHOT</version>  <relativePath>../edu-common-parent</relativePath> </parent> <groupId>wusc.edu.service</groupId> <artifactId>edu-service-user</artifactId> <version>${edu-service-user.version}</version> <packaging>jar</packaging> <name>edu-service-user</name> <url>http://maven.apache.org</url> <properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <log4j.leve>debug</log4j.leve>  <log4j.ale>debug</log4j.ale> </properties> <build>  <finalName>edu-service-user</finalName>  <resources>          <!-- 把src/main/resources下的包括xml和properties的文件复制到target/classes下 -->   <resource>    <targetPath>${project.build.directory}/classes</targetPath>    <directory>src/main/resources</directory>    <filtering>true</filtering>    <includes>     <include>**/*.xml</include>     <include>**/*.properties</include>    </includes>   </resource>   <!-- 把src/main/resources/spring下的spring-context.xml   文件复制到target/classes/META-INF/spring下 -->   <resource>    <targetPath>${project.build.directory}/classes/META-INF/spring</targetPath>    <directory>src/main/resources/spring</directory>    <filtering>true</filtering>    <includes>     <include>spring-context.xml</include>    </includes>   </resource>  </resources>  <pluginManagement>    <!--这个插件没看懂  似乎和eclipse与mavan生命周期冲突相关  -->   <plugins>    <plugin>     <groupId>org.eclipse.m2e</groupId>     <artifactId>lifecycle-mapping</artifactId>     <version>1.0.0</version>     <configuration>      <lifecycleMappingMetadata>       <pluginExecutions>        <pluginExecution>         <pluginExecutionFilter>          <groupId>org.apache.maven.plugins</groupId>          <artifactId>maven-dependency-plugin</artifactId>          <versionRange>[2.0,)</versionRange>          <goals>           <goal>copy-dependencies</goal>          </goals>         </pluginExecutionFilter>         <action>          <ignore />         </action>        </pluginExecution>       </pluginExecutions>      </lifecycleMappingMetadata>     </configuration>    </plugin>   </plugins>  </pluginManagement>  <plugins>    <!--打jar包范围是rarget/classes  启动类是 com.alibaba.dubbo.container.Main  -->   <plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-jar-plugin</artifactId>    <configuration>     <classesDirectory>target/classes</classesDirectory>     <archive>      <manifest>       <!-- 不记录版本号 -->       <useUniqueVersions>false</useUniqueVersions>       <addClasspath>true</addClasspath>        <classpathPrefix>lib/</classpathPrefix>       <mainClass>com.alibaba.dubbo.container.Main</mainClass>      </manifest>      <manifestEntries>       <Class-Paht>.</Class-Paht>      </manifestEntries>     </archive>    </configuration>   </plugin>   <!--打jar包 项目本身的依赖包放在lib文件夹下   (lib与生成的jar在同一目录)  -->   <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>       <type>jar</type>       <includeTypes>jar</includeTypes>       <useUninqeVersions>false</useUninqeVersions>       <outputDirectory>        ${project.build.directory}/lib       </outputDirectory>      </configuration>     </execution>    </executions>   </plugin>  </plugins> </build> <dependencies>  <!-- dubbo 需要的jar start -->  <dependency>   <groupId>org.jboss.netty</groupId>   <artifactId>netty</artifactId>  </dependency>  <dependency>   <groupId>com.alibaba</groupId>   <artifactId>dubbo</artifactId>   <exclusions>    <exclusion>     <groupId>org.springframework</groupId>     <artifactId>spring</artifactId>    </exclusion>   </exclusions>  </dependency> </dependencies></project>

</pre>如果这个项目本身还依赖另一个项目A,那么我们就得先把项目A打成jar包使用上面的pom,然后在myeclipse中右键,run as maven install最后结果<p>如下图</p><p>lib下存放着edu-service-user运行所需要的jar</p><p>另外,我们之间在命令行下,使用java -jar edu-service-user.jar 即可调用</p><p>com.alibaba.dubbo.container.Main这个类</p><p><img src="https://img-blog.csdn.net/20160502222634711?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


第二种方式

<pre name="code" class="html"><plugin>      <groupId>org.apache.maven.plugins</groupId>      <artifactId>maven-shade-plugin</artifactId>      <version>1.2.1</version>      <executions>          <execution>              <phase>package</phase>              <goals>                  <goal>shade</goal>              </goals>              <configuration>                  <transformers>                      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">                          <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>                      </transformer>                  </transformers>              </configuration>          </execution>      </executions>  </plugin>  
运行mvn install 后 会在target下生成两个jar包

original-xxx-0.0.1-snapshot.jar 这里面只有我们自己写的java代码的class文件

xxx-0.0.1-snapshot.jar 这里面我们依赖的jar包(会抽出里面的class文件)会和我们的代码放在一块

如下:

使用maven将项目打成jar包
toyprogram里面放的是我们自己写的java代码的class文件

spring就放在org里面



还有一种方式

<plugin>              <artifactId>maven-assembly-plugin</artifactId>              <configuration>                  <appendAssemblyId>false</appendAssemblyId>                  <descriptorRefs>                      <descriptorRef>jar-with-dependencies</descriptorRef>                  </descriptorRefs>                  <archive>                      <manifest>                          <mainClass>abc.ef.sdf</mainClass>                      </manifest>                  </archive>              </configuration>              <executions>                  <execution>                      <id>make-assembly</id>                      <phase>package</phase>                      <goals>                          <goal>assembly</goal>                      </goals>                  </execution>              </executions>  </plugin>  

然后在pom文件上右键 run as->maven assembly:assembly
之后在pom的同级目录里会生成你所需要的jar

不知道怎么回事,我的myeclipse里没有maven assembly:assembly

不过可以这样run build... 然后

使用maven将项目打成jar包



参考资料

http://www.blogjava.net/qileilove/articles/410887.html

http://www.cnblogs.com/justinzhang/p/4983633.html