提出问题:
使用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
后记:只做记录,供以后查询方便。欢迎访客一起交流。
相关推荐
maven打包 maven打jar包详细步骤
Maven打包,指定classes路径打包到WebContent/WEB-INF中
自己整理的maven打包出现错误的解决办法、含有截图等 附带maven搭建私服等资料
sprintboot maven 打包分离lib jar 资源文件 properties xml yml 详细信息查看我的博客 https://mp.csdn.net/postedit/80274087 java -jar -cp 启动
idea新建maven web项目.zip Jetbrains IntelliJ IDEA创建基于maven打包工具的WEB网站项目 本项目使用的是SSM框架spring mvc,spring, mybatis.用maven打包成jar
当前案例中包含一整套的代码和word文档,非常适合新手... 主要是通过maven打包加载不同环境的properties文件 然后将对于的属性绑定到指定的实体对象中;然后通过调用接口可以看到加载不同环境控制台打印的内容是不一样的
jasperreport 用maven打包后找不到字体解决方案 net.sf.jasperreports.engine.JRRuntimeException: Could not load the following font
maven打包源码配置
当前案例中包含一整套的代码和word文档,非常适合新手代码简单易懂; 主要是通过maven打包配合springboot application.properties文件配置实现通过打包来完成加载不同环境的配置内容;
maven 打包 部署到tomcat 删除的jar包,无法访问jsp的情况
这是一个springboot+maven写的demo,主要演示springboot项目通过maven插件,将依赖与配置文件打包到jar包外部,实现灵活的项目管理.仅供参考学习
工程运行环境有研发,测试和生产,不同的运行环境配置不同,maven 打包时指定文件目录,使用指定的文件夹下的配置文件。
maven打包使用yui压缩css和js文件
NULL 博文链接:https://skyteam.iteye.com/blog/1896973
maven打包dubbo服务接口(maven-assembly-plugin)
Maven 打包资源. 与文章配套食用. Maven 打包实战 https://blog.csdn.net/u010416101/article/details/88539238
allatori第二代Java代码混淆器Maven打包配置,依赖库,以及配置文件。示例运行,可行。
maven打包main方法作为起动程序在maven框架下,写的main方法需要打包在jdk环境运行 文档主要描述打包的方法
NULL 博文链接:https://yizhilong28.iteye.com/blog/1146830
使用Maven打包spark项目