@tsing1226
2016-11-17T19:39:28.000000Z
字数 2596
阅读 1055
maven
Java项目完成后需要进行打包发布,下面主要是运用maven进行打成tar包。详细pom文件请参考如下实例:
simaplepom.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>
<!--这里只介绍build时的部分-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.lakala.datamigration.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/assembly/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>
在Java项目根目录中创建:mkdir src/main/assembly
(也可以收到创建)
Assembly.xml
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>bin-${version}</id>
<formats>
<format>tar</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!--将打好的jar包放在lib目录下-->
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>src/main/resources/conf</directory>
<outputDirectory>conf</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
<lineEnding>unix</lineEnding>
</fileSet>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>README.txt</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>run.sh</include>
</includes>
<fileMode>0775</fileMode>
</fileSet>
</fileSets>
</assembly>
在Java根目录编译:maven clean compile package -Dmaven.test.skip=true
注意:编译好的jar包和依赖包都在lib文件夹下。
执行脚本的命令如下:
#! /usr/bin/env bash
#start the daemodes
echo "Start the process."
SHELL_CURRENT=`dirname "${BASH_SOURCE-$0}"`
PROJECT_BIN=`cd "$SHELL_CURRENT"; pwd`
PROJECT_HOME=`dirname "$PROJECT_BIN"`
echo "${PROJECT_HOME}"
#
export JAVA_HOME=${JAVA_HOME}
java -cp ${PROJECT_HOME}/lib/*:. com.xxx.xxx.Main