maven打包一直都困扰着我这种新手,现在我也有点明白这是一个什么东西了。把maven打包问题总结一下。
首先配置好各种东西,能运行打包之后再细细研究一下这个打包的配置文件。
pom.xml文件:
添加各种jar包引用不多讲了
下来是配置主类这个东西:pom.xml中添加<bulid>
内容如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.sysware.HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
<span style="white-space:pre"> </span><resources>
<span style="white-space:pre"> </span> <resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include> 把配置文件等也打到包内
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</plugin>
这样就可以对简单工程进行打包,但是,如果有各种各样的文件存在,需要放到包内或者包外面,则需要自定义打包的情况,用assembly插件打包
根据需要,我们又要把一些文件打到包外, 放到和包同级的文件夹内,配置文件变为如下:
<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>demo</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>demo.App</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/assemble/package.xml</descriptor> 多了这个描述标签,需要新建src/main/assemble/package.xml文件
</descriptors> 这个文件就是assembly打包要描述的,配置一下你需要另外打包的文件夹,各种
<span style="white-space:pre">
</span> 文件等,我新加文件夹conf里面放conf.xml文件
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources> 这一段是给jar包里面把配置文件加入
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
<assembly 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/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/java</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>conf</directory> 需要打包的文件夹
<outputDirectory>config</outputDirectory> 输出的文件夹
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory> 这个放置依赖jar包,以后再研究
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
如果程序中需要读取xml文件或配置文件,不放在jar包内,就把配置文件这样打包出来,程序内部的路径要用classloader或者System.setProperties();
具体上一个博文中都介绍了,两个一起用,解决打自定义包和外部配置文件引用问题
原文地址:http://blog.csdn.net/u014405427/article/details/37612361