简体   繁体   English

使用gradle构建一个非常简单的java程序

[英]Using gradle to build a very simple java program

I am new to Java and Gradle, and have a very newbie question.我是 Java 和 Gradle 的新手,有一个非常新手的问题。 I have the following Java file:我有以下 Java 文件:

public class TestMain {
    public static void main(String[] args) {
        System.out.println("Hello.....");
    }
}

I am able to compile the above file using javac, and run it using command "java TestMain".我能够使用 javac 编译上述文件,并使用命令“java TestMain”运行它。

I am now trying to do the same using gradle build framework.我现在正在尝试使用 gradle 构建框架来做同样的事情。 I performed the following steps: run "gradle init --type java-library copied the above file into src/main/java/我执行了以下步骤:运行“gradle init --type java-library 将上述文件复制到 src/main/java/

When I run "./gradlew build", I get a TestMain.class file, and a also a "building-java-file.jar" (the whole gradle directory is in building-java-file directory).当我运行“./gradlew build”时,我得到一个 TestMain.class 文件,还有一个“building-java-file.jar”(整个 gradle 目录在 building-java-file 目录中)。

$ ls -l build/classes/main/TestMain.class 
-rw-r--r--  1 user1  foo\eng  610 May 22 17:22 build/classes/main/TestMain.class

$ java  build/classes/main/TestMain
Error: Could not find or load main class build.classes.main.TestMain

How do I run the TestMain.class?我如何运行 TestMain.class? Also, what is the reason for gradle creating the jar file - building-java-file.jar?另外,gradle 创建 jar 文件的原因是什么 - building-java-file.jar?

Btw, my build.gradle file is pretty empty.顺便说一句,我的 build.gradle 文件非常空。

apply plugin: 'java'

repositories {
    jcenter()
}

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

    testCompile 'junit:junit:4.12'
}

Thank you, Ahmed.'谢谢你,艾哈迈德。

Answers to my question are explained clearly in the Gradele documentation site.我的问题的答案在 Gradele 文档站点中有清楚的解释。

Basically, start with: gradle init --type java-application基本上,从: gradle init --type java-application

Link: https://guides.gradle.org/building-java-applications/?_ga=2.79084180.165016772.1527029076-64181247.1527029076链接: https : //guides.gradle.org/building-java-applications/?_ga=2.79084180.165016772.1527029076-64181247.1527029076

Use JavaExec.使用 JavaExec。 As an example put the following in build.gradle作为示例,将以下内容放入 build.gradle

task execute(type:JavaExec) {
   main = mainClass
   classpath = sourceSets.main.runtimeClasspath
}

To run gradle -PmainClass=Boo execute.运行 gradle -PmainClass=Boo 执行。 You get你得到

$ gradle -PmainClass=Boo execute
:compileJava
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes
:execute
I am BOOMMMM!

mainClass is a property passed in dynamically at command line. mainClass 是在命令行动态传入的属性。 classpath is set to pickup the latest classes. classpath 设置为拾取最新的类。

If you do not pass in the mainClass property, this fails as expected.如果您没有传入 mainClass 属性,这将按预期失败。

$ gradle execute

FAILURE: Build failed with an exception.

* Where:
Build file 'xxxx/build.gradle' line: 4

* What went wrong:
A problem occurred evaluating root project 'Foo'.
> Could not find property 'mainClass' on task ':execute'.

I think you are looking for a solution to create a runnable jar.我认为您正在寻找创建可运行 jar 的解决方案。 Here is the solution what you can add it in the build.gradle这是您可以在 build.gradle 中添加的解决方案

  • Execute gradle init --type java-application执行gradle init --type java-application

  • Add the following gradle task to the build.gradle.将以下 gradle 任务添加到 build.gradle。

  • Runnable fat Jar (with all dependent libraries copied to the jar) Runnable fat Jar(将所有依赖库复制到 jar 中)

     task fatJar(type: Jar) { manifest { attributes 'Main-Class': 'com.example.gradle.App' } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } with jar }

Runnable jar with all dependencies copied to a directory and adding the classpath to the manifest将所有依赖项复制到目录并将类路径添加到清单的可运行 jar

def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
    from configurations.compile
    into "${dependsDir}"
}
task createJar(dependsOn: copyDependencies, type: Jar) {
  
    manifest {
        attributes('Main-Class': 'com.example.gradle.App',
                'Class-Path': configurations.compile.collect { 'dependencies/' + it.getName() }.join(' ')
        )
    }
    with jar
}

How to use ?如何使用 ?

Add the above tasks to build.gradle将上述任务添加到 build.gradle
Execute gradle fatJar //create fatJar执行gradle fatJar //创建 fatJar
Execute gradle createJar // create jar with dependencies copied.执行gradle createJar // 创建 jar 并复制依赖项。

A complete article, which I wrote on this topic, can be read here : A simple java project with Gradle我在这个主题上写的一篇完整的文章,可以在这里阅读: A simple java project with Gradle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM