maven + assembly 构建 java项目(jar)
2016-11-23 10:08
134 查看
在这里记录maven + assembly构建发布完整的jar 项目 assembly是什么本文不加多解释,网上很多介绍,主要介绍构建存在过程。
需要项目必须是一个maven项目,然后在pom.xml中加入assembly插件(如下代码),其中src/assembly/repository.xml是assembly具体的配置文件
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptors> <descriptor>src/assembly/repository.xml</descriptor> </descriptors> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build>
配置repository.xml
<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>jar-with-dependencies</id> <baseDirectory>${project.artifactId}-${project.version}</baseDirectory> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <directory>${basedir}/bin</directory> <fileMode>755</fileMode> <excludes> <exclude>*.classpath</exclude> </excludes> <outputDirectory>/bin</outputDirectory> </fileSet> <fileSet> <directory>${basedir}/target</directory> <includes> <include>${artifactId}-${version}.jar</include> </includes> <outputDirectory>/lib</outputDirectory> </fileSet> <fileSet> <directory>${basedir}/bin</directory> <includes> <include>*.classpath</include> </includes> <outputDirectory>/bin</outputDirectory> </fileSet> <fileSet> <directory>${basedir}/conf</directory> <outputDirectory>/conf</outputDirectory> <includes> <include>**/*.properties</include> </includes> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>/lib</outputDirectory> </dependencySet> </dependencySets> </assembly>
这样就可以完成maven + assembly,随之发现一个问题,项目的配置文件需要放置在外面,只要是数据库 配置和。。。。 因此配置文件需要单独出来“conf”,需要一个 读取properties的java,还需要log4j配置文件的加载
package bestv.ydjd.tool; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; /** * .properties文件操作类 * * */ public final class JPropertiesUtil { private static String DEFALUT_PROPERTIES_FILE = "conf/config.properties"; private static Properties p = new Properties(); static { String filePath = DEFALUT_PROPERTIES_FILE; FileInputStream in = null; try { in = new FileInputStream(filePath); if (in != null) { p.load(in); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { } } } /** * 取得配置文件的值 * * @param key * 键 * @return 值 */ public static String getProperties(String key) { if (isNullOrEmpty(key)) { throw new IllegalArgumentException("参数(key)为空。"); } String rtn = p.getProperty(key); if (rtn == null) { throw new IllegalArgumentException("配置文件参数(" + key + ")没有设定。"); } return rtn; } public static int getPropertiesForInt(String key) { if (isNullOrEmpty(key)) { throw new IllegalArgumentException("参数(key)为空。"); } String rtn = p.getProperty(key); if (rtn == null) { throw new IllegalArgumentException("配置文件参数(" + key + ")没有设定。"); } int result = Integer.parseInt(rtn); return result; } public static boolean isNullOrEmpty(String value) { if (value == null) { return true; } if (value.trim().equals("")) { return true; } return false; } }
import org.apache.log4j.PropertyConfigurator; /** * * @author laughing * @date 2016-11-22 15:08:44 * @description 加载log4j.properties 需要在使用的时候调用LoadLog4jProperties */ public class LoadLog4jProperties { static { PropertyConfigurator.configure(System.getProperty("user.dir") + "/conf/log4j.properties"); } }
最后只需要写一个linux 的启动脚本即可
#!/bin/bash cd `dirname $0` cd .. DEPLOY_DIR=`pwd` LIB_DIR=$DEPLOY_DIR/lib LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"` nohup java -classpath $CONF_DIR:$LIB_JARS$CLASSPATH bestv.ydjd.job.UploadProgram > stdout.log &
相关文章推荐
- Jenkins之——构建Java Maven项目(Jar)并发布到远程服务器
- DOS命令行中用MAVEN构建Java和Java Web项目
- 使用assembly将maven项目pom.xml中的jar包打包
- maven命令构建自己的java项目
- Myeclipse 10 Maven 构建 Java Web 项目
- Java-Maven项目引入UEditor图片上传组件jar包类库的5种方式
- java项目构建工具Maven
- 构建maven Java项目
- 使用maven构建,打包Java项目完整实例
- 将java项目使用maven打成jar包,可在doc窗口直接运行
- 使用Maven构建Java项目
- DOS命令行中用MAVEN构建 Java 和 Java Web 项目
- maven将java项目中依赖的jar包合并到最终的jar中
- 【Java Web】Myeclipse下运用maven管理项目jar包
- 利用maven构建和管理java项目
- DOS命令行中用MAVEN构建 Java 和 Java Web 项目
- maven构建的项目 发布jar到nexus上
- Maven构建一个简单的Java项目
- 使用Maven开发编译Java项目时如何将所有依赖jar包都打入war包
- Java-Maven项目引入UEditor图片上传组件jar包类库的5种方式