Just do your best!!!

Maven打包jar文件读取jar包外的配置文件和传入参数

前提

  1. 准备一个banner.txt文件

maven配置文件

<properties>
        <exec.mainClass>com.main.Main</exec.mainClass>
</properties>

<build>
        <plugins>
            <!--指定maven编译时的jdk版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!--跳过打包是的测试-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${exec.mainClass}</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Main.java文件

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by WJY on 2017/5/5.
 */
public class Main {

    //读取banner
    private static void readBanner(String path){
        try{
            File file = new File(path+"banner.txt");
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
            if(file.exists()){
                BufferedReader br = new BufferedReader(reader);
                String s = null;
                while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                    System.out.println(s);
                }
                br.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try{
            //获取jar包的路径
            String filePath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            //去除jar包的名称
            int last = filePath.lastIndexOf("/");
            String path = filePath.substring(0,last)+"/";
            System.out.println(path);

            //读取banner
            readBanner(path);
            //打印输入的参数
            System.out.println(Arrays.deepToString(args));

            Scanner sc = new Scanner(System.in);
            String str = sc.next();
            System.exit(0);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

执行Maven Lifecycle的package命令,打包项目为jar包,把banner.txt放到jar包的目录下(文件夹最好不要带中文),然后cmd执行:java -jar xxx.jar appTest 查看参数是否传入