您的位置:首页 > 编程语言 > Java开发

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 &
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven jar assembly log4j java