使用maven生成可执行的jar包的方法
程序员文章站
2023-12-19 13:15:58
本文介绍了使用maven生成可执行的jar包的方法,分享给大家,具体如下:
从pom的xsi中可以打开描述pom的schema:
可以看到pom中,project的...
本文介绍了使用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] ------------------------------------------------------------------------
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。