在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序。最近在工作中也遇到了这种场景,在做定时任务通常会编译jar包工程,部署运行,程序会依赖很多jar包,项目初始化时,只需要初始化主函数及运行相关程序。
使用方法:
(1)手动打包maven工程pom文件配置:
<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>com.bj58.opt</groupId>
<artifactId>com.bj58.opt.cron</artifactId>
<version>5.0.0-SNAPSHOT</version>
<!--packaging>war</packaging-->
<name>jingzhun-cron</name>
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://10.58.120.19:8058/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Internal Snapshots</name>
<url>http://10.58.120.19:8058/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>com.bj58.opt.cpt</groupId>
<artifactId>com.bj58.opt.cpt.contract</artifactId>
<version>4.0.41</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<finalName>jingzhun-cron</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
(2)assembly.xml文件配置:
<assembly>
<id>cron</id>
<formats>
<format>tar.gz</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<outputDirectory>/bin</outputDirectory>
<directory>src/main/bin</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet>
<outputDirectory>/config</outputDirectory>
<directory>src/main/config</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
<fileSet>
<outputDirectory>/conf</outputDirectory>
<directory>src/main/conf</directory>
<includes>
<include>**/*.*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
(3) maven install后最终的jar包结构为:(若想添加更多文件结构,可以修改assembly.xml文件配置)