使用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 ...
随机推荐
- HDU-1878 欧拉回路 欧拉回路
题目链接:https://cn.vjudge.net/problem/HDU-1878 题意 中文题,而且就是单纯的欧拉回路 思路 判断连通图 用并查集会很好,bfs亦可 一时脑抽用bfs过了这个题, ...
- Symfony4中文文档: 安装和设置Symfony框架
安装和设置Symfony框架 要创建新的Symfony应用程序, 首先确保使用的是PHP7.1 或更高版本并且已经安装Componser. 如果未安装, 请首先在系统上全局安装Componser. 如 ...
- Spring拦截器 /* 和 /** 的区别
SpringMVC 拦截器拦截 /* 和 /** 的区别: /* : 匹配一级,即 /add , /query 等 /** : 匹配多级,即 /add , /add/user, /add/user/u ...
- Select For update语句浅析
Select -forupdate语句是我们经常使用手工加锁语句.通常情况下,select语句是不会对数据加锁,妨碍影响其他的DML和DDL操作.同时,在多版本一致读机制的支持下,select语句也不 ...
- To new is C++; To malloc is C; To mix them is sin (混淆C++中的new和C中的malloc是一种犯罪)
Introduction One of the most common questions that get asked during interviews for C++ programmers i ...
- POJ 1636 DFS+DP
思路: 先搜索出来如果选这个点 其它哪些点必须选 跑个背包就好了 //By SiriusRen #include <cstdio> #include <cstring> #in ...
- Sub Thread to update main Thread (UI)
Sub Thread to update main Thread (UI) main Thread : A has Hander.HandleMessage() to process the & ...
- TwinCAT 3中基于UDP协议通讯的C++实现
因为项目需要,学习了TwinCAT3中使用UDP协议进行通讯的基本知识.这个做个简单的笔记,方便以后查询. 1 概述 倍福为了实现从实时环境中直接访问网卡(network cards)专门提供了一个函 ...
- Ubuntu16.04 “有线未托管”有线网络不可用问题解决
Ubuntu16.04 “有线未托管”问题解决 电脑上安装的Ubuntu16.04 是通过先安装Ubuntu Server后在通过命令 sudo tasksel 安装的Gnome桌面环境,安装完成后发 ...
- BZOJ1367: [Baltic2004]sequence(左偏树)
Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...