Gradle配置Java工程

前言

本篇文章主要介绍下如何使用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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,523评论 25 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 转载注明出处://www.greatytc.com/p/5255b100930e 0. 前言 完全由个人翻...
    王三的猫阿德阅读 2,575评论 0 4
  • 运动会结束之后,所有参加运动会比赛的运动员,他们的心中都有一些感想,他们愿意把自己的感想同大家分享,他们这种精神,...
    允诺亿笙阅读 638评论 0 1