为配合自动化部署hudson,最近研究了如何将eclipse maven工程打包成可运行的jar函数及对应的资源文件。
由于我们工程中包含了多个可运行的任务,在打包成jar时需要分别导出,pom提到版本库需要一致,不能部署一个任务提交一次代码。所以考虑了在进行mvn打包时通过传递参数控制导出jar中的main所在的类。在pom中定义properties,在执行命令时传递参数即可。如mvn package -DtaskFinalName=contentpoolurl -DmainClassName=com.ifec.blueair.task.contentpoolurl.ContentPoolUrlRun,另外在通过mvn导出jar找不到配置文件,发现没有找到当前目录,通过加入manifestEntries即可指定目录。maven生成的MANIFEST.MF中的Class-Path的内容缺少一些内容,比如当前执行目录(.),那么可以通过上面manifestEntries的方式增加进来。
pom.xml内容如下:
<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>
<parent>
<groupId>com.ifec.blueair</groupId>
<artifactId>blueair-packagemanager</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../blueair-packagemanager/pom.xml</relativePath>
</parent>
<artifactId>blueair-task</artifactId>
<packaging>jar</packaging>
<name>blueair-task</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<propertiesName>contentpoolurl</propertiesName>
<taskFinalName>contentpoolurl</taskFinalName>
<mainClassName>com.ifec.blueair.task.contentpoolurl.ContentPoolUrlRun</mainClassName>
</properties>
<build>
<finalName>${taskFinalName}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.sh</include>
<include>**/${propertiesName}.properties</include>
<include>**/log4j.properties</include>
<include>**/config.properties</include>
</includes>
<filtering>true</filtering>
<targetPath>${project.build.directory}</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${taskFinalName}_lib</classpathPrefix>
<mainClass>${mainClassName}</mainClass>
</manifest> <!--maven生成的MANIFEST.MF中的Class-Path的内容缺少一些内容,比如当前执行目录(.),那么可以通过上面manifestEntries的方式增加进来。-->
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/${taskFinalName}_lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>