Gradle 打可執行jar包


初次使用Gradle,想和maven一樣,把gradle項目打成可執行jar包,具體步驟:

1、下載gradle 版本,並配置環境變量, 下載地址:https://gradle.org/releases/

  再cmd下能執行gradle命令

2、idea 中新建一個gradle項目

3、build.gradle文件配置如下:

  

apply plugin:'java'
apply plugin:'application'
jar{
    manifestContentCharset 'utf-8'
    metadataCharset 'utf-8'
    manifest{
        // manifest 文件聲明主程序入口
        attributes "Main-Class":"com.test.TestHello"
    }

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

}

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

sourceCompatibility = 1.8

repositories {
    //mavenCentral()
    //配置阿里雲mavnen 庫
    maven{
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
}
dependencies {
    // 添加項目依賴,這里添加了selenium
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'

}

4、寫一個java 主程序,就是打印hello gradle,並且遍歷下傳給main方法的字符串數組

package com.test;

public class TestHello {
    public static void main(String[] args) {
        System.out.println("Hello Gradle!");
        for (String s: args){
            System.out.println(s);
        }
    }
}

5、再項目根目錄下面執行命令:gradle jar

C:\Users\think\IdeaProjects\Hello>gradle jar

BUILD SUCCESSFUL in 6s
2 actionable tasks: 2 executed
C:\Users\think\IdeaProjects\Hello>

6、在項目目錄下,build\libs 文件夾下有個jar包:Hello-1.0-SNAPSHOT.jar

執行這個jar包並傳入字符串數組,會正確打印傳入的字符串數組

 

轉載於:https://www.cnblogs.com/testway/p/10204898.html

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM