maven 学习 十 关于打包

时间:2022-06-27 09:57:14

clean package -Dmaven.test.skip=true -P product

这个命令干的活: 清class文件,打包构建,跳过测试,注意最后一个 -P product, 会激活项目下的pom.xml配置的<profiles>标签下id为product。

Maven提供了Profile的概念来决绝不同环境打包的问题:

<profiles>
<profile>
<id>kaifa</id>
<properties>
<db.url>192.10.2.168</db.url>
<db.username>dbtest</db.username>
<db.password>dbtest</db.password>
</properties>
</profile> <profile>
<id>shengchan</id>
<properties>
<db.url>192.20.1.11</db.url>
<db.username>admin</db.username>
<db.password>comfreesecurity</db.password>
</properties>
</profile>
</profiles>

 常用插件:

  • maven-jar-plugin

打成jar时,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中。

(classpath:classpath是Java运行时环境搜索类和其他资源文件(比如jar\zip等资源)的路径。可以通过JDK工具(比如javac命令、java命令)后面的-

classpath 参数设置classpath)

 <plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>play.core.server.ProdServerStart</mainClass> // 主函数
<addClasspath>true</addClasspath> // 加入到classpath
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<!-- Add config folder into classpath of MANIFEST -->
<manifestEntries>
<Class-Path>conf/</Class-Path> // 资源文件也加入到classpath
</manifestEntries>
</archive>
<classesDirectory></classesDirectory>
<!--<excludes> <exclude>*.conf</exclude> <exclude>*.xml</exclude>
</excludes> -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>/data/lib</classpathPrefix>
<mainClass>com.zhang.spring.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
  • maven-dependency-plugin

            用于复制依赖的jar包到指定的文件夹里,

  <plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
  • maven-assembly-plugin

自定义打包方式。

 <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor> // 具体的打包方式定义到assembly.xml文件中
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<assembly>
<id>dist</id>
<formats>
<format>zip</format> // 指定打包的格式
</formats>
<!-- set to false the archive created will unzip its content to the current directory. -->
<includeBaseDirectory>false</includeBaseDirectory> <fileSets>
<!-- copy file from target folder -->
<fileSet>
<directory>${basedir}/target</directory> // 把 这个路径下的所有*.jar文件打包到指定目录
<includes>
<include>*.jar</include>
</includes>
<outputDirectory></outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/target/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>${basedir}/target/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>600</fileMode>
<lineEnding>unix</lineEnding>
</fileSet> <!-- copy file from source folder -->
<fileSet>
<directory>${basedir}/dist</directory>
<outputDirectory></outputDirectory>
<fileMode>744</fileMode>
<lineEnding>unix</lineEnding>
<includes>
<include>start</include>
<include>stop</include>
</includes>
</fileSet>
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>logback-test.xml</exclude>
</excludes>
<outputDirectory>conf</outputDirectory>
<fileMode>600</fileMode>
<lineEnding>unix</lineEnding>
</fileSet>
</fileSets> <!-- <files>
<file>
<source>${basedir}/dist/README</source>
</file>
</files>-->
</assembly>