脚本命令加载外部配置文件夹conf

加载log4j配置文件 Log4iConfigurer类

public class Log4iConfigurer {
    
    private static boolean INITIALIZED = false;
    
    public static void initLogger(){
        if(!INITIALIZED&&!isConfigured()){
            InputStream is =Log4iConfigurer.class.getClassLoader().getResourceAsStream("log4j.properties");
            PropertyConfigurator.configure(is);
            IOUtils.closeQuietly(is);
        }
    }

    private static boolean isConfigured() {
        if(LogManager.getRootLogger().getAllAppenders().hasMoreElements()){
            return true;
        }else{
            Enumeration<?> loggers =LogManager.getCurrentLoggers();
            while(loggers.hasMoreElements()){
                Logger logger= (Logger)loggers.nextElement();
                if(logger.getAllAppenders().hasMoreElements()){
                    return true;
                }
            }
        }
        return false;
    }
    
}

读取配置文件类Util

public class Util {
    
    private static final Logger logger = LoggerFactory.getLogger(Util.class);
    
    /**mapping.properties file name*/
    public static final Properties props = new Properties();
    public static final String CONF_PROPERTIES_FILE= "mapping.properties";
    public static final String CONF = "RUN_CONF";
    
    static{
        Log4iConfigurer.initLogger();
        getProperties();
    }
    
    public static String GetString(String key){
        String value = null;
        value = props.getProperty(key);
        return value;
    }
    
    //读取配置文件
    private static File getConfProperties(){
        String confHome = System.getProperty(CONF);
        if(!StringUtils.isEmpty(confHome)){
            logger.info("Use CONF="+confHome);
            return getPropertiesFile(confHome);
        }
        
        logger.warn("Conf property was not set ,will seek conf env variable");
        
        String runHome = getRunHome();
        if(StringUtils.isEmpty(runHome)){
            throw new RuntimeException("didn't find project runpath,please set");
        }
        String path = runHome+File.separator+"conf";
        return getPropertiesFile(path);
    }
    
    public static void getProperties(){
        File propFile = getConfProperties();
        if(propFile == null||!propFile.exists()){
            logger.info("fail to load properties");
            throw new RuntimeException("fail to load properties");
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(propFile);
            props.load(fis);
        } catch (IOException e) {
             throw new RuntimeException(e);
        } finally {
             IOUtils.closeQuietly(fis);
        }
    }
    
    private static File getPropertiesFile(String path){
        if(path == null){
            return null;
        }
        return new File(path,CONF_PROPERTIES_FILE);
    }
    
    private static String getRunHome(){
        String runHome = System.getProperty("runpath");
        if(StringUtils.isEmpty(runHome)){
            logger.warn("run home was not set");
        }
        return runHome;
    }
    
    
    public static void main(String[] args) {
        System.out.println(System.getenv("PATH"));
        System.out.println(System.getProperty("conf"));
        System.setProperty("run.home", "/home/dinpay/mappinghome");
        System.out.println(System.getProperty("run.home"));
    }
    
}

打包导出

   

另外附上window和linux的启动脚本命令

startup.cmd

echo start elasticsearch-mapping
java -Drunpath=C:Usersll-t150Desktopesmap -Dlog4j.configuration=conf/log4j.properties -cp lib/* com.dinpay.bdp.rcp.service.Start 
pause 

startup.sh

echo start elasticsearch-mapping
java -Drunpath=/home/dinpay/esmap -Dlog4j.configuration=conf/log4j.properties -classpath .:lib/* com.dinpay.bdp.rcp.service.Start
echo "create elasticsearch mapping successfully!"
原文地址:https://www.cnblogs.com/atomicbomb/p/6625130.html