Gradle构建JAR的行为与从IDE正常执行不同

时间:2018-07-13 09:33:59

标签: java gradle apache-poi

我一直在研究Java企业应用程序(app.jar)。它使用config.xlsx作为配置文件并运行应用程序,将执行run.sh脚本(脚本为app.jar提供了所有必需的参数)。

目录结构:

run.sh app.jar config.xlsx

要运行该应用程序

./run.sh

我正在编写负载生成器代码,该代码将循环更改config.xlsx文件并为每种配置执行run.sh脚本。

我的查询是,当我从IDE(IntelliJ)运行Load Generator应用程序时,它运行良好。但是我的Gradle构建JAR在第一次迭代中停止执行。而且我也没有例外。共享我的LoadGenerator类,build.gradle和输出。

LoadGenerator.java

    public class LoadGenerator {
        public static final String CONFIGURATION_FILE_PATH = "config.xlsx";

        public static void main(String[] args) throws IOException{
            System.out.println("Going to change config file 10 times...");
            File file = new File(CONFIGURATION_FILE_PATH);
            FileInputStream fileInputStream = new FileInputStream(file);
            Workbook workbook = new XSSFWorkbook(fileInputStream);

            Sheet sheet = workbook.getSheetAt(4);

            Row idRow = sheet.getRow(10);
            Row portRow = sheet.getRow(15);

            Cell idCell = idRow.getCell(1);
            Cell portCell = portRow.getCell(1);

            System.out.println("Old id: " + idCell + " | " + "Old port: " + portCell);


            int port = 2000;

            for (int i = 0; i < 10; i++) {
                // Change cell values
                int newPort = port + i;
                String newId = "TEST_ID_" + newPort;
                idCell.setCellValue(newId);
                portCell.setCellValue(newPort);

                System.out.println("New id: " + idCell + " | " + "New port: " + portCell);


                // Execution stops here without throwing exception
                FileOutputStream fileOut = new FileOutputStream(file);

                workbook.write(fileOut);
                fileOut.flush();
                fileOut.close();

                //Code to Run Shell command below
                //....
            }
        }
    }

build.gradle

plugins {
    id 'java'
}

group 'com.rizz'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.apache.poi:poi:3.17"
    compile "org.apache.poi:poi-ooxml:3.17"
}

clean.doFirst {
    delete "${rootDir}\\out\\"
    println "${rootDir}\\out\\"
}


jar {
    manifest {
        attributes "Main-Class": "com.load.LoadGenerator"
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}


task customFatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.load.LoadGenerator'
    }
    baseName = 'all-in-one-jar'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

输出

Going to change config file 10 times...
Old id: TEST_ID_1000 | Old port: 1000.0
New id: TEST_ID_2000 | New port: 2000.0

0 个答案:

没有答案