maven + assembly 构建 java项目(jar)

在这里记录maven + assembly构建发布完整的jar 项目
assembly是什么本文不加多解释,网上很多介绍,主要介绍构建存在过程。
  1. 需要项目必须是一个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>
  1. 配置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 &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值