美文网首页
Gradle配置Java工程

Gradle配置Java工程

作者: Whyn | 来源:发表于2017-08-24 17:00 被阅读45次

前言

本篇文章主要介绍下如何使用Gradle来配置Java工程。
主要简单介绍下手动配置流程,避免其它复杂的步骤,尽量争取简洁明了。

手动配置

  1. 首先,你需要下载一个Gradle可执行文件,解压后将路径添加到系统环境变量中。

    environment_path
    配合完成后,在控制台输入:gradle -v,如果显示下图所示信息,那么就说明Gradle已经被成功安装到系统中了。
    gradle_version
  2. 接下来,手动创建一个Java工程根目录:mkdir java-demo

  3. 进入java-demo目录内,手动创建子目录结构:
    mkdir -p src/main/java/com/yn/test

    tree.PNG
  4. 然后我们就可以创建Java源文件:

package com.yn.test;

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Build Java Project by Gradle");
    }
}

  1. 在工程根目录下创建一个build.gradle文件,进行配置:
/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.0.1/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java' 

// Apply the application plugin to add support for building an application
apply plugin: 'application' 

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
 //   compile 'com.google.guava:guava:21.0'

    // Use JUnit test framework
//    testCompile 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'com.yn.test.HelloWorld'
  1. 最后,执行gradle build进行编译:
    gradle_build
  2. 经过以上步骤,我们便成功地使用Gradle完成了对Java工程的配置了,最后,使用gradle run就可以运行我们的Java程序了:
gradle_run

自动配置

  1. 手动创建一个Java工程根目录:mkdir java-demo
  2. 运行: gradle init --type java-application

更多详情,请参考: Building Java Applications

参考

Building Java Applications
Building Java Projects with Gradle

相关文章

网友评论

      本文标题:Gradle配置Java工程

      本文链接:https://www.haomeiwen.com/subject/xveedxtx.html