From:https://www.linux178.com/Java/maven-release.html

用Java写了一个命令行的小程序,使用的Intellij IDE是IDEA13原来一直使用Ant来打包编译,为了学习一下maven打包,特此从Ant打包转转换为Maven打包发布

源码的目录结构如下:

conf 目录是 程序的配置文件所在的目录
lib 程序的依赖库文件
scripts 启动程序的叫,start.bat 是在window上启动的脚本,start.sh是在Linux启动的脚本
src 是源代码目录
build.xml 是Ant打包的脚本

build.xml内容如下:

<project default="jar" name="easyxms">
<!-- 工程目录结构
project
|-conf
|-lib
|-logs
|-scripts
|-src
|-build.xml
-->
<property name="lib.dir" value="lib"/>
<property name="src.dir" value="src"/>
<property name="classes.dir" value="bin"/>
<property name="output.dir" value="easyxms"/>
<property name="scripts.dir" value="scripts"/>
<property name="jarname" value="easyxms.jar"/>
<property name="mainclass" value="org.easyxms.EasyXMS"/>
<property name="orginal.conf.dir" value="conf"/>
<property name="orginal.logs.dir" value="logs"/>
<property name="logs.dir" location="${output.dir}/${orginal.logs.dir}"/>
<property name="conf.dir" location="${output.dir}/${orginal.conf.dir}"/> <!-- 第三方jar包的路径 -->
<path id="lib-classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path> <!-- 1. 初始化工作,如创建目录等 -->
<target name="init">
<mkdir dir="${classes.dir}"/>
<mkdir dir="${output.dir}"/>
<mkdir dir="${logs.dir}"/>
<mkdir dir="${conf.dir}"/>
</target> <!-- 2. 编译 -->
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}">
<compilerarg line="-encoding UTF-8"/>
<classpath refid="lib-classpath"/>
</javac>
<copy todir="${conf.dir}">
<fileset dir="${orginal.conf.dir}" excludes=".svn" />
</copy>
<copy todir="${output.dir}">
<fileset dir="${scripts.dir}" excludes=".svn" />
</copy>
<copy file="${src.dir}/log4j.properties" todir="${classes.dir}"/>
<copy file="${src.dir}/commons-logging.properties" todir="${classes.dir}"/>
</target> <!-- 3. 打包jar文件 -->
<target name="jar" depends="compile">
<copy todir="${output.dir}/lib">
<fileset dir="${lib.dir}"/>
</copy> <!--Create a property containing all .jar files,
prefix lib/, and seperated with a space-->
<pathconvert property="mf.classpath" pathsep=" ">
<mapper>
<chainedmapper>
<!-- jar包文件只留文件名,去掉目录信息 -->
<flattenmapper/>
<!-- add lib/ prefix -->
<globmapper from="*" to="lib/*"/>
</chainedmapper>
</mapper>
<path refid="lib-classpath"/>
</pathconvert> <!-- jar文件的输出路径 -->
<jar destfile="${output.dir}/${jarname}" basedir="${classes.dir}">
<manifest>
<attribute name="Main-class" value="${mainclass}"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>
</target>
</project>

打包之后的结果:

二、使用Maven来打包发布

源码结构如下:

因为我没有测试的代码,就不需要 src/test目录

src/main 下的所有目录作用:

assemble/release.xml 这个是maven-assembly-plugin 这个插件使用的描述符文件,用来自定义打包发布(自定义发布包的目录结果)
conf 程序的配置文件目录
db 程序生产的数据库文件目录,这里面的.gitignore文件,是为了让git追踪该目录的,git默认情况是不追踪空目录的,这个目录是程序运行后会在里面创建数据库文件,所以需要该目录
example 添加Ip时的示例excel文件
java 源代码目录
logs 程序生成的日志文件目录,跟db目录类似,这个是程序运行产生的日志
resources 程序资源目录,比如log4j的配置文件log4j.properties,这个目录下的文件打成jar包的时候,会发到jar包的根目录下面,打包的过程中会放到 target/classes目录下
scripts 启动程序的脚本文件
pom.xml Maven定义项目依赖的文件

这其中
pom.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.easyxms</groupId>
<artifactId>easyxms</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <!-- 定制项目信息 -->
<name>easyxms</name>
<url>http://www.easyxms.org</url> <licenses>
<license>
<name>Apache 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses> <organization>
<name>easyxms</name>
<url>http://www.easyxms.org</url>
</organization> <developers>
<developer>
<id>L</id>
<name>LEO</name>
<email>chanyipiaomiao@163.com</email>
<url>http://www.easyxms.org</url>
<organization>easyxms</organization>
<organizationUrl>http://www.easyxms.org</organizationUrl>
<roles>
<role>developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers> <!-- 定义依赖库文件 -->
<dependencies> <dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency> <dependency>
<groupId>jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.4.2</version>
</dependency> <dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency> <dependency>
<groupId>it.sauronsoftware.base64</groupId>
<artifactId>javabase64</artifactId>
<version>1.3.1</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency> <dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies> <!-- 编译打包 -->
<build> <!-- 控制资源文件的拷贝 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</resources> <plugins> <!-- 解决资源文件的编码问题 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <!-- 设置源文件编码方式 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>org.easyxms.EasyXMS</mainClass>
</manifest>
</archive>
</configuration>
</plugin> <!-- 自定义发布版本包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version> <executions>
<execution>
<id>create-release-zip</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions> <configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble/release.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins> </build>
</project>

release.xml 文件内入如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>release package</id>
<formats>
<format>zip</format>
</formats> <!-- 打zip包时,包含一层打包目录 -->
<includeBaseDirectory>true</includeBaseDirectory> <!-- 包含程序运行自身所需的目录 -->
<fileSets>
<fileSet>
<directory>src/main/conf</directory>
<outputDirectory>/conf</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/logs</directory>
<outputDirectory>/logs</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/example</directory>
<outputDirectory>/example</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/db</directory>
<outputDirectory>/db</outputDirectory>
</fileSet>
</fileSets> <!-- 把编译好的jar文件包含到发布的目录中去并设置脚本文件的权限-->
<files>
<file>
<source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
<destName>easyxms.jar</destName>
<outputDirectory>/</outputDirectory>
</file>
<file>
<source>src/main/scripts/start.sh</source>
<!-- 设置start.sh文件的换行符为unix换行符\n -->
<lineEnding>unix</lineEnding>
<outputDirectory>/</outputDirectory>
<fileMode>0740</fileMode>
</file>
<file>
<source>src/main/scripts/start.bat</source>
<!-- 设置start.bat文件的换行符为dos换行符\r\n -->
<lineEnding>dos</lineEnding>
<outputDirectory>/</outputDirectory>
</file>
</files> <!-- 复制所有的依赖jar到发布的目录的lib目录下 -->
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory> <!--不要把主程序本身包含进lib目录,如果不加这个主程序的jar包也会包含到lib目录下-->
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets> </assembly>

命令行进入到 F:\workspace\Java\EasyXMS 项目的目录

然后执行 mvn clean package,执行过程如下:

再到target目录下,就可以看到

easyxms-1.0.0.jar 这个项目主程序
easyxms-1.0.0.zip 是发布给别人使用的包,结构如下:

里面包含了项目的主程序,自此项目就从Ant打包转换为了Maven来打包发布了。

 

使用Ant 和 Maven打包发布命令行程序(转载)的更多相关文章

  1. [原] Android自动打包之命令行打包

    Android自动打包流程详细图: 总结为以下几个步骤: 1. 生成R文件 2. Java代码编译成class文件 3. class文件生成dex文件 4. 打包资源 5. 生成apk 6. 创建密匙 ...

  2. Node: 开发命令行程序

    CLI 的全称是 Command-line Interface (命令行界面),即在命令行接受用户的键盘输入并作出响应和执行的程序. 在 Node.js 中,全局安装的包一般都具有命令行界面的功能,例 ...

  3. dotnet 使用 System.CommandLine 写命令行程序

    在写命令行程序的时候,会遇到命令行解析的问题,以及参数的使用和规范化等坑.现在社区开源了命令行项目,可以帮助小伙伴快速开发命令行程序,支持自动的命令行解析和规范的参数 我写过一篇关于命令行解析的博客C ...

  4. 手写笔记变PDF-几行代码变命令行程序为图形化界面

    前言 最近发现了一个非常不错的Python类库----Gooey, https://github.com/chriskiehl/Gooey 在它的帮助下我们可以非常方便的将一个命令行程序升级成一个图形 ...

  5. Node.js 命令行程序开发教程

    nodejs开发命令行程序非常方便,具体操作方式查看下面几篇文章 http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html ...

  6. 在 Mac OS X 上创建的 .NET 命令行程序访问数据库 (使用Entity Framework 7 )

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  7. C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数

    控制台程序(命令行程序)设置窗口宽度高度,如下代码: Console.WriteLine(Console.WindowHeight); Console.WriteLine(Console.Buffer ...

  8. 命令行程序增加 GUI 外壳

    Conmajia © 2012 Updated on Feb. 21, 2018 命令行大家都用过: 图 1 命令行程序工作界面 现在想办法为它做一个 GUI 外壳,实际效果参考图 2. 图 2 带 ...

  9. myapp——自动生成小学四则运算题目的命令行程序(侯国鑫 谢嘉帆)

    1.Github项目地址 https://github.com/baiyexing/myapp.git 2.功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 功能(已全部实现) 使用 -n ...

随机推荐

  1. [Codeforces 841C]Leha and Function

    题目大意:定义函数F(n,k)为[1,2,3,..n]中k个元素的子集中最小元素的数学期望.现在给你两个长度相等的数列A,B(A中元素严格大于B中元素),现在要你重新排列A,使得$\sum\limit ...

  2. docker常用命令,学习笔记

    - 常用命令 https://docs.docker.com images > docker images # 查看本地镜像 > docker images -a # 查看所(含中间镜像层 ...

  3. gcc/g++命令参数笔记

    1. gcc -E source_file.c -E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c -S,只执行到源代码到汇编代码的转换,输出汇编代码. 3. ...

  4. Chrome无界面浏览模式与自定义插件加载问题

    环境:Python 3.5.x + Selenium 3.4.3 + Chromedriver 2.30 + Chrome 60 beta或Chromium Canary 61 + WIN10 Chr ...

  5. No enclosing instance of type E is accessible.

    No enclosing instance of type E  is accessible. 静态方法(main)中调用内部类,会出现这样的问题: 学习了:https://www.cnblogs.c ...

  6. Find problem in eXtremeDB

    class table1 { char<8>    f1; char<80>  f2; uint4        f3; uint4        f4; double     ...

  7. 计数排序、桶排序python实现

    计数排序在输入n个0到k之间的整数时,时间复杂度最好情况下为O(n+k),最坏情况下为O(n+k),平均情况为O(n+k),空间复杂度为O(n+k),计数排序是稳定的排序. 桶排序在输入N个数据有M个 ...

  8. centos 项目上线shell脚本

    最近在弄项目上线,然后写了个上线,备份,回滚的shell脚本 上线可根据自己公司项目做相关操作,备份回滚可修改目录则可实现 主管要求用shell写,那就用shell写吧 本想Python写更好的 哈哈 ...

  9. Centos7 网络出错(failed to start LSB: Bring up/down networking )

    这是我更换了VM虚拟机内存,重启后无法连接网络. 然后这是因为NetworkManager.service这个程序造成 解决方法: systemctl disable NetworkManager.s ...

  10. BZOJ 1577 贪心

    思路:同POJ3038 http://blog.csdn.net/qq_31785871/article/details/52953214 //By SiriusRen #include <se ...