`
阅读更多

提出问题:

           使用maven打包非常的方便,那么如何进行war和jar的打包呢?

解决方案:

      一、打war包

          1. 使用maven打war包非常的简单,只需要maven install即可。

          2.指定jdk版本:

<!-- 指定编译器1.6;原因是行里WAS只支持JDK1.6 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.1</version>
	<configuration>
		<source>1.6</source>
		<target>1.6</target>
	</configuration>
</plugin>

         二、打jar包,

          1. 打可执行jar包,我们更加喜欢将第三方jar打在一起,如下:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<configuration>
	<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
	<execution>
		<phase>package</phase>
		<goals>
			<goal>shade</goal>
		</goals>
		<configuration>
			<transformers>
				<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
					<mainClass>com.web.java.ftptar.YadaTimer</mainClass>
				</transformer>
			</transformers>
		</configuration>
	</execution>
</executions>
</plugin>

         2. 上面的插件很方便的打一个可执行jar包,但是如果包含配置文件,我们又希望配置文件放在外面,如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.web.java.ftptar.YadaTimer</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <!--被编译过的应用程序class文件存放的目录。-->
                            <outputDirectory>${project.build.directory}/etc</outputDirectory>
                            <resources>
                                <!--这个元素描述了项目相关或测试相关的所有资源路径-->
                                <resource>
                                    <!--描述存放资源的目录,该路径相对POM路径-->
                                    <directory>src/main/resources/</directory>
                                    <!--包含的模式列表,例如**/*.xml.-->
                                    <includes>
                                        <exclude>**/*.properties</exclude>
                                        <exclude>**/*.xml</exclude>
                                        <exclude>**/*.bat</exclude>
                                    </includes>
                                    <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- Jar 插件包含建立Jar文件的目标 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Configuration of the archiver -->
                    <archive>
                        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>

                        <!-- Manifest specific configuration -->
                        <manifest>
                            <!-- 是否要把第三方jar放到manifest的classpath中 -->
                            <addClasspath>true</addClasspath>
                            <!-- 生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/ -->
                            <classpathPrefix>/</classpathPrefix>
                            <!-- 应用的main class -->
                            <mainClass>com.web.java.ftptar.YadaTimer</mainClass>
                        </manifest>
                        <!-- 用maven在MANIFEST.MF资料中的Class-Path中增加当前目录(.)  -->
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--排除的模式列表,例如**/*.xml-->
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.bat</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!--plugin元素包含描述插件所需要的信息。-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!--Compiler 插件包含编译源代码和单元测试代码的目标-->
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <!-- “编码 GBK 的不可映射字符”问题的解决 -->
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

         3. 打可执行jar包,如果包含多个模块,我们想把依赖jar放在lib文件夹下,如下:

<!--构建项目需要的信息-->
    <build>

        <!--使用的插件列表 。-->
        <plugins>

            <!--plugin元素包含描述插件所需要的信息。-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!--Compiler 插件包含编译源代码和单元测试代码的目标-->
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <!-- “编码 GBK 的不可映射字符”问题的解决 -->
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <!--不写匹配默认规则,TestSuit结尾,test开头的方法  -->
                        <include>**/*TestSuit.java</include>   <!-- 两个星号**用来匹配任意路径,一个星号*用来获取除路径风格符外的0个或多个字符 -->
                    </includes>
                    <excludes>
                        <exclude>**/CollectionBillTestCase.java</exclude>
                        <exclude>**/PaymentBillTestCase.java</exclude>
                    </excludes>
                    <skip>false</skip>  <!-- 略过单元测试 -->
                    <testFailureIgnore>true</testFailureIgnore> <!-- 当Maven 遇到一个测试失败,它默认的行为是停止当前的构建。 如果你希望继续构建项目,即使 Surefire 插件遇到了失败的单元测试,你就需要设置 Surefire 的testFailureIgnore 这个配置属性为 true -->
                </configuration>
            </plugin>

            <!--scala 插件编译scala源代码的目标-->
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->
                <executions>
                    <!--execution元素包含了插件执行需要的信息-->
                    <execution>
                        <!--配置的执行目标-->
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <!--作为DOM对象的配置-->
                <configuration>
                    <args>
                        <arg>-feature</arg>
                    </args>
                </configuration>
            </plugin>
            <!-- Jar 插件包含建立Jar文件的目标 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <!-- Configuration of the archiver -->
                    <archive>
                        <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                        <addMavenDescriptor>false</addMavenDescriptor>

                        <!-- Manifest specific configuration -->
                        <manifest>
                            <!-- 是否要把第三方jar放到manifest的classpath中 -->
                            <addClasspath>true</addClasspath>
                            <!-- 生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/ -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <!-- 应用的main class -->
                            <mainClass>com.bjyada.rps.appmain.RpsApp</mainClass>
                        </manifest>
                        <!-- 用maven在MANIFEST.MF资料中的Class-Path中增加当前目录(.)  -->
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <!--排除的模式列表,例如**/*.xml-->
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                            <!--被编译过的应用程序class文件存放的目录。-->
                            <outputDirectory>${project.build.directory}/etc</outputDirectory>
                            <resources>
                                <!--这个元素描述了项目相关或测试相关的所有资源路径-->
                                <resource>
                                    <!--描述存放资源的目录,该路径相对POM路径-->
                                    <directory>src/main/resources/</directory>
                                    <!--包含的模式列表,例如**/*.xml.-->
                                    <includes>
                                        <exclude>**/*.properties</exclude>
                                        <exclude>**/*.xml</exclude>
                                    </includes>
                                    <!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- 把依赖的jar包拷到lib目录下 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->
                <executions>
                    <!--execution元素包含了插件执行需要的信息-->
                    <execution>
                        <!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->
                        <id>copy-dependencies</id>
                        <!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->
                        <phase>package</phase>
                        <!--配置的执行目标-->
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!--被编译过的应用程序class文件存放的目录。-->
                            <outputDirectory>
                                ${project.build.directory}/etc/lib
                            </outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <excludeArtifactIds>junit</excludeArtifactIds>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

   注意: 如果用exe4j打包,出现配置文件路径错误问题,看看.class.getClassLoader().getResource

   后记:只做记录,供以后查询方便。欢迎访客一起交流。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics