插件1:生成可执行jar
Maven Shade Plugin for the simple convenience of making the JAR file executable.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>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>hello.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
常用插件
<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>org.apache.maven</groupId> <artifactId>Plugins</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Plugins</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <!-- 生成的jar包的名称 --> <finalName>pluginDemo</finalName> <!-- pluginManagement用于在parent中对插件进行统一配置,然后在子POM中直接引用 如果没有父子POM关系,则直接使用<plugins/>进行插件配置即可。 --> <pluginManagement> </pluginManagement> <!-- 注意:插件可以不指定版本,Maven会自动匹配一个较新的插件进行使用 --> <plugins> <!-- 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <!-- compatible with JVM 1.6 --> <source>1.6</source> <target>1.6</target> <!-- Default value is: ${project.build.sourceEncoding}. --> <encoding>UTF-8</encoding> <!-- running the compiler in a separate process --> <fork>true</fork> <meminitial>128m</meminitial> <maxmem>512m</maxmem> </configuration> </plugin> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <!-- Default value is: ${project.build.sourceEncoding}. --> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 生成源码包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.2.1</version> <configuration> <!-- The directory where the generated archive file will be put. Default value is: ${project.build.directory}. --> <outputDirectory>${project.build.directory}</outputDirectory> <!-- Specifies whether or not to include the POM file in the sources-jar. Default value is: false. --> <includePom>true</includePom> <!-- The filename to be used for the generated archive file --> <finalName>filename-of-generated-jar-file</finalName> <!-- Specifies whether or not to attach the artifact to the project Default value is: true. --> <attach>false</attach> </configuration> <executions> <execution> <phase>package</phase> <goals> <!-- generate a stand alone source jar --> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- 测试插件 --> <!-- JAXWS 插件 --> <!-- Jetty 插件 --> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
插件2: 拷贝所有依赖jar包到指定目录中(该插件会到私服下载所有依赖包,包括传递依赖)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/dependency</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin>
插件3:与插件1类似,用来指定MANIFEST文件,然后用java -jar xxx.jar运行jar包。由于jar包可能存在很多依赖,因此可以用插件2将所有依赖都下载下来,然后执行。
addClassPath=true设置运行时的类路径,通过classpathPrefix指定依赖包的路径。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <finalName>ccb</finalName> <archive> <manifest> <mainClass> com.gc.bas.adminconsole.FrameApplication </mainClass> <addClasspath>true</addClasspath> <classpathPrefix> ./dependency/ </classpathPrefix> </manifest> </archive> </configuration> </plugin>