[关闭]
@tsing1226 2016-11-17T19:39:28.000000Z 字数 2596 阅读 1055

maven

运用maven对java项目进行打包


概述

Java项目完成后需要进行打包发布,下面主要是运用maven进行打成tar包。详细pom文件请参考如下实例:


pom文件书写

simaplepom.xml,此部分只是打包部分不包含加载软件依赖等信息。


  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <!--这里只介绍build时的部分-->
  5. <build>
  6. <plugins>
  7. <plugin>
  8. <groupId>org.apache.maven.plugins</groupId>
  9. <artifactId>maven-compiler-plugin</artifactId>
  10. <version>3.5.1</version>
  11. <configuration>
  12. <source>1.8</source>
  13. <target>1.8</target>
  14. </configuration>
  15. </plugin>
  16. <plugin>
  17. <artifactId>maven-assembly-plugin</artifactId>
  18. <configuration>
  19. <descriptorRefs>
  20. <descriptorRef>jar-with-dependencies</descriptorRef>
  21. </descriptorRefs>
  22. <archive>
  23. <manifest>
  24. <mainClass>com.lakala.datamigration.Main</mainClass>
  25. </manifest>
  26. </archive>
  27. <descriptors>
  28. <descriptor>src/main/assembly/assembly.xml</descriptor>
  29. </descriptors>
  30. </configuration>
  31. <executions>
  32. <execution>
  33. <id>make-assembly</id>
  34. <phase>package</phase>
  35. <goals>
  36. <goal>single</goal>
  37. </goals>
  38. </execution>
  39. </executions>
  40. </plugin>
  41. </plugins>
  42. </build>
  43. </project>

在Java项目根目录中创建:mkdir src/main/assembly(也可以收到创建)

Assembly.xml

  1. <assembly
  2. xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  5. <id>bin-${version}</id>
  6. <formats>
  7. <format>tar</format>
  8. </formats>
  9. <includeBaseDirectory>true</includeBaseDirectory>
  10. <dependencySets>
  11. <dependencySet>
  12. <!--将打好的jar包放在lib目录下-->
  13. <useProjectArtifact>true</useProjectArtifact>
  14. <outputDirectory>lib</outputDirectory>
  15. </dependencySet>
  16. </dependencySets>
  17. <fileSets>
  18. <fileSet>
  19. <directory>src/main/resources/conf</directory>
  20. <outputDirectory>conf</outputDirectory>
  21. <includes>
  22. <include>*.xml</include>
  23. <include>*.properties</include>
  24. </includes>
  25. <lineEnding>unix</lineEnding>
  26. </fileSet>
  27. <fileSet>
  28. <outputDirectory>/</outputDirectory>
  29. <includes>
  30. <include>README.txt</include>
  31. </includes>
  32. </fileSet>
  33. <fileSet>
  34. <directory>src/main/scripts</directory>
  35. <outputDirectory>bin</outputDirectory>
  36. <includes>
  37. <include>run.sh</include>
  38. </includes>
  39. <fileMode>0775</fileMode>
  40. </fileSet>
  41. </fileSets>
  42. </assembly>

编译

在Java根目录编译:maven clean compile package -Dmaven.test.skip=true
注意:编译好的jar包和依赖包都在lib文件夹下。


任务发布执行脚本

执行脚本的命令如下:

  1. #! /usr/bin/env bash
  2. #start the daemodes
  3. echo "Start the process."
  4. SHELL_CURRENT=`dirname "${BASH_SOURCE-$0}"`
  5. PROJECT_BIN=`cd "$SHELL_CURRENT"; pwd`
  6. PROJECT_HOME=`dirname "$PROJECT_BIN"`
  7. echo "${PROJECT_HOME}"
  8. #
  9. export JAVA_HOME=${JAVA_HOME}
  10. java -cp ${PROJECT_HOME}/lib/*:. com.xxx.xxx.Main
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注