使用Ant 和 Maven打包发布命令行程序(转载)
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打包发布命令行程序(转载)的更多相关文章
- [原] Android自动打包之命令行打包
Android自动打包流程详细图: 总结为以下几个步骤: 1. 生成R文件 2. Java代码编译成class文件 3. class文件生成dex文件 4. 打包资源 5. 生成apk 6. 创建密匙 ...
- Node: 开发命令行程序
CLI 的全称是 Command-line Interface (命令行界面),即在命令行接受用户的键盘输入并作出响应和执行的程序. 在 Node.js 中,全局安装的包一般都具有命令行界面的功能,例 ...
- dotnet 使用 System.CommandLine 写命令行程序
在写命令行程序的时候,会遇到命令行解析的问题,以及参数的使用和规范化等坑.现在社区开源了命令行项目,可以帮助小伙伴快速开发命令行程序,支持自动的命令行解析和规范的参数 我写过一篇关于命令行解析的博客C ...
- 手写笔记变PDF-几行代码变命令行程序为图形化界面
前言 最近发现了一个非常不错的Python类库----Gooey, https://github.com/chriskiehl/Gooey 在它的帮助下我们可以非常方便的将一个命令行程序升级成一个图形 ...
- Node.js 命令行程序开发教程
nodejs开发命令行程序非常方便,具体操作方式查看下面几篇文章 http://www.ruanyifeng.com/blog/2015/05/command-line-with-node.html ...
- 在 Mac OS X 上创建的 .NET 命令行程序访问数据库 (使用Entity Framework 7 )
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数
控制台程序(命令行程序)设置窗口宽度高度,如下代码: Console.WriteLine(Console.WindowHeight); Console.WriteLine(Console.Buffer ...
- 命令行程序增加 GUI 外壳
Conmajia © 2012 Updated on Feb. 21, 2018 命令行大家都用过: 图 1 命令行程序工作界面 现在想办法为它做一个 GUI 外壳,实际效果参考图 2. 图 2 带 ...
- myapp——自动生成小学四则运算题目的命令行程序(侯国鑫 谢嘉帆)
1.Github项目地址 https://github.com/baiyexing/myapp.git 2.功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 功能(已全部实现) 使用 -n ...
随机推荐
- java编程思想--学习心得
学习Java编程思想,需要了解语言特性,对于各种名词,能够借助项目代码,解释其含义,不借助搜索工具,明白其在什么样场景下使用,会带来什么样的问题,能否避免这类问题. 学习的过程,与软件开发相同,一样是 ...
- Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)
Pre-installation requirements1. Java2. Python steps1. Download latest version of Tomcat (Tomcat 7) f ...
- CSU 1364 Interview RMQ
题意: 瑶瑶有一家有一家公司,最近他想招m个人.因为他的公司是如此的出名,所以有n个人来参加面试.然而,瑶瑶是如此忙,以至于没有时间来亲自面试他们.所以他准备选择m场面试来测试他们. 瑶瑶决定这样来安 ...
- 【Codeforces Round #239 (Div. 1) A】Triangle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后的直角三角形可以通过平移,将直角顶点移动到坐标原点. 然后我们只要枚举另外两个点其中一个点的坐标就好了. x坐标的范围是[1.. ...
- Watcher详解 工作机制, Watcher客户端注册、Watcher 服务端注册
Watcher详解.接口 在 ZooKeeper 中, 接口类 Watcher 用于表示一个标注你的事件处理器,其定义了事件通知相关的逻辑,包含 KeeperState 和 EventType 两个枚 ...
- No enclosing instance of type E is accessible.
No enclosing instance of type E is accessible. 静态方法(main)中调用内部类,会出现这样的问题: 学习了:https://www.cnblogs.c ...
- 欢迎各位技术牛人增加Swift QQ群:343549891
急招:五年以上Swift开发经验,24个月工资.30天年假.配司机专车. 欢迎各位技术牛人增加Swift 敏捷大拇指 官方QQ群1: 报上"来自CSDN"就可以.谢谢! 訪问 大拇 ...
- Protocol Buffers的基础说明和使用
我们開始须要使用protobuf的原因是,在处理OJ的contest模块的时候,碰到一个问题就是生成contestRank的时候.须要存储非常多信息. 假设我们採用model存储的话,那么一方面兴许假 ...
- STL使用————SET&MULTISET
SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...
- 5. webservice通信调用天气预报接口实例
转自:https://blog.csdn.net/xiejuan6105/article/details/78452605 一:环境搭建 1:新建一个java project工程weatherInf ...