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. vsftp迁移记录笔记

     由于之前的服务器用的window下的ftp安全性和稳定性都不好,所以我们才把ftp迁移到linux环境下 vsftp概述: vsftpd 它可以运行在多平台系统上面,是一个完全免费的.开放源代码的f ...

  2. Bash 基础特性

    命令别名  alias 显示当前shell中定义的所有别名  alias 别名='原始命令'  unalias 别名 取消定义的别名在命令前加\使用命令本身,而不是别名(或者使用绝对路径执行命令使用命 ...

  3. 以替换为主的疯狂填词、sub()介绍

    去年接到一个任务,一直给拖到了今天,再这么下去可不行,今天我就要让你们看看我的厉害 任务是这样的:创建一个程序,读入文本文件,并让用户在该文本出现ADJECTIVE .NOUN.ADVERB或VERB ...

  4. 紫书 例题 10-26 UVa 11440(欧拉函数+数论)

    这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...

  5. Android开发之Menu:OptionMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)

    菜单的概念,现在已经很普及了.Windows系统.Mac.桌面版Linux.Java Swing等,都有可视化菜单.一.Android平台3种菜单  选项菜单(OptionMenu).上下文菜单(Co ...

  6. 用JS中的cookie实现商品的浏览记录

    最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...

  7. POJ——T 1988 Cube Stacking

    http://poj.org/problem?id=1988 Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 25865   ...

  8. 使用glPushMatrix和glPopMatrix的原因

    转自 百度百科   glPushMatrix 函数将当前矩阵堆栈推送,通过一个,复制当前矩阵. 这就是后 glPushMatrix 的调用堆栈的顶部矩阵是它下面的相同的.   1. 原理讲解 终于明白 ...

  9. [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

    Rather than using Components to push streams into other Components, mapPropsStream allows you to cre ...

  10. 2015上海网络赛 HDU 5475 An easy problem 线段树

    题意就不说了 思路:线段树,维护区间乘积.2操作就将要除的点更新为1. #include<iostream> #include<cstdio> #include<cstr ...