使用maven生成可执行的jar包的方法

所属分类: 软件编程 / java 阅读数: 23
收藏 0 赞 0 分享

本文介绍了使用maven生成可执行的jar包的方法,分享给大家,具体如下:

从pom的xsi中可以打开描述pom的schema:

可以看到pom中,project的结构:

默认的mvn install生成的jar是不带主类入口的,需要在maven-compile-plugin中设置主类,

<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">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.cetc.di</groupId>
 <artifactId>hellocetc</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>hellocetc</name>
 <url>http://maven.apache.org</url>



 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 
 
<build>
<pluginManagement>
<plugins>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <archive>
      <manifest>
        <mainClass>com.cetc.di.hellocetc.App</mainClass>
        <addClasspath>true</addClasspath>
      <classpathPrefix>lib/</classpathPrefix>
      </manifest>

    </archive>
    <classesDirectory>
    </classesDirectory>
  </configuration>
</plugin>
</plugins>  
</pluginManagement>
</build> 
</project>

执行mvn install:

在target目录中,发现jar包已经生成:

用java decompiler,可以看到manifest中已经加入了MainClass:

使用mvn help:effective-pom可以看到pom.xml的完整结构(包括继承而来的属性):

[INFO] Scanning for projects...
[INFO]                                     
[INFO] ------------------------------------------------------------------------
[INFO] Building hellocetc 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-help-plugin:2.2:effective-pom (default-cli) @ hellocetc ---
[INFO] 
Effective POMs, after inheritance, interpolation, and profiles are applied:

<!-- ====================================================================== -->
<!--                                    -->
<!-- Generated by Maven Help Plugin on 2015-11-18T08:05:12         -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/        -->
<!--                                    -->
<!-- ====================================================================== -->

<!-- ====================================================================== -->
<!--                                    -->
<!-- Effective POM for project 'com.cetc.di:hellocetc:jar:0.0.1-SNAPSHOT'  -->
<!--                                    -->
<!-- ====================================================================== -->

<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">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.cetc.di</groupId>
 <artifactId>hellocetc</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>hellocetc</name>
 <url>http://maven.apache.org</url>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <repositories>
  <repository>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
   <id>central</id>
   <name>Central Repository</name>
   <url>https://repo.maven.apache.org/maven2</url>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
   <releases>
    <updatePolicy>never</updatePolicy>
   </releases>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
   <id>central</id>
   <name>Central Repository</name>
   <url>https://repo.maven.apache.org/maven2</url>
  </pluginRepository>
 </pluginRepositories>
 <build>
  <sourceDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\src\main\java</sourceDirectory>
  <scriptSourceDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\src\main\scripts</scriptSourceDirectory>
  <testSourceDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\src\test\java</testSourceDirectory>
  <outputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\classes</outputDirectory>
  <testOutputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\test-classes</testOutputDirectory>
  <resources>
   <resource>
    <directory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\src\main\resources</directory>
   </resource>
  </resources>
  <testResources>
   <testResource>
    <directory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\src\test\resources</directory>
   </testResource>
  </testResources>
  <directory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target</directory>
  <finalName>hellocetc-0.0.1-SNAPSHOT</finalName>
  <pluginManagement>
   <plugins>
    <plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.3</version>
    </plugin>
    <plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <version>2.2-beta-5</version>
    </plugin>
    <plugin>
     <artifactId>maven-dependency-plugin</artifactId>
     <version>2.8</version>
    </plugin>
    <plugin>
     <artifactId>maven-release-plugin</artifactId>
     <version>2.3.2</version>
    </plugin>
    <plugin>
     <artifactId>maven-jar-plugin</artifactId>
     <version>2.4</version>
     <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <archive>
       <manifest>
        <mainClass>com.cetc.di.hellocetc.App</mainClass>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
       </manifest>
      </archive>
      <classesDirectory />
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
  <plugins>
   <plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <executions>
     <execution>
      <id>default-clean</id>
      <phase>clean</phase>
      <goals>
       <goal>clean</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
     <execution>
      <id>default-testResources</id>
      <phase>process-test-resources</phase>
      <goals>
       <goal>testResources</goal>
      </goals>
     </execution>
     <execution>
      <id>default-resources</id>
      <phase>process-resources</phase>
      <goals>
       <goal>resources</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
     <execution>
      <id>default-jar</id>
      <phase>package</phase>
      <goals>
       <goal>jar</goal>
      </goals>
      <configuration>
       <source>1.8</source>
       <target>1.8</target>
       <archive>
        <manifest>
         <mainClass>com.cetc.di.hellocetc.App</mainClass>
         <addClasspath>true</addClasspath>
         <classpathPrefix>lib/</classpathPrefix>
        </manifest>
       </archive>
       <classesDirectory />
      </configuration>
     </execution>
    </executions>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
     <archive>
      <manifest>
       <mainClass>com.cetc.di.hellocetc.App</mainClass>
       <addClasspath>true</addClasspath>
       <classpathPrefix>lib/</classpathPrefix>
      </manifest>
     </archive>
     <classesDirectory />
    </configuration>
   </plugin>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <executions>
     <execution>
      <id>default-compile</id>
      <phase>compile</phase>
      <goals>
       <goal>compile</goal>
      </goals>
     </execution>
     <execution>
      <id>default-testCompile</id>
      <phase>test-compile</phase>
      <goals>
       <goal>testCompile</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <executions>
     <execution>
      <id>default-test</id>
      <phase>test</phase>
      <goals>
       <goal>test</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.4</version>
    <executions>
     <execution>
      <id>default-install</id>
      <phase>install</phase>
      <goals>
       <goal>install</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.7</version>
    <executions>
     <execution>
      <id>default-deploy</id>
      <phase>deploy</phase>
      <goals>
       <goal>deploy</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
   <plugin>
    <artifactId>maven-site-plugin</artifactId>
    <version>3.3</version>
    <executions>
     <execution>
      <id>default-site</id>
      <phase>site</phase>
      <goals>
       <goal>site</goal>
      </goals>
      <configuration>
       <outputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\site</outputDirectory>
       <reportPlugins>
        <reportPlugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-project-info-reports-plugin</artifactId>
        </reportPlugin>
       </reportPlugins>
      </configuration>
     </execution>
     <execution>
      <id>default-deploy</id>
      <phase>site-deploy</phase>
      <goals>
       <goal>deploy</goal>
      </goals>
      <configuration>
       <outputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\site</outputDirectory>
       <reportPlugins>
        <reportPlugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-project-info-reports-plugin</artifactId>
        </reportPlugin>
       </reportPlugins>
      </configuration>
     </execution>
    </executions>
    <configuration>
     <outputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\site</outputDirectory>
     <reportPlugins>
      <reportPlugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-project-info-reports-plugin</artifactId>
      </reportPlugin>
     </reportPlugins>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <reporting>
  <outputDirectory>D:\Users\a\Workspaces\MyEclipse 2015\hellocetc\target\site</outputDirectory>
 </reporting>
</project>

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.526 s
[INFO] Finished at: 2015-11-18T20:05:12+08:00
[INFO] Final Memory: 10M/245M
[INFO] ------------------------------------------------------------------------

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多