maven打可执行jar包的几种build写法
1、指定Main方法,依赖jar放进另一个文件夹中,并将该文件夹设为classpath
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.2</version> <configuration> <archive> <manifest> <!--change addClasspath to true if daks is desktop version--> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.liuyx.Main</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> <manifestEntries> <Class-Path>.</Class-Path> <!--<Permissions>${Permissions}</Permissions> <Caller-Allowable-Codebase>${Caller-Allowable-Codebase}</Caller-Allowable-Codebase>--> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <configuration> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> <goals> <goal>copy-dependencies</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
命令:
mvn package
结果会将所有依赖jar包放进lib目录下。
MANIFEST.MF内容(示例):
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: Voldemort Build-Jdk: 1.8.0_144 Specification-Title: refreshIp Specification-Version: 1.0-SNAPSHOT Implementation-Title: refreshIp Implementation-Version: 1.0-SNAPSHOT Implementation-Vendor-Id: com.liuyx Main-Class: com.liuyx.Main Class-Path: . lib/httpclient-4.5.2.jar lib/httpcore-4.4.4.jar lib/comm ons-logging-1.2.jar lib/commons-codec-1.9.jar lib/fastjson-1.2.29.jar
2、将所有内容放到一个jar包里
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <mainClass>com.liuyx.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <!--(end) for package jar with dependencies --> </plugins> </build>
命令:
mvn package
结果会将所有依赖jar包打进:xxx-jar-with-dependencies.jar