debian中设置tomcat开机启动

基于gradle构建java应用程序

妙音 posted @ 2018年6月30日 23:33 in java with tags gradle , 2066 阅读
 
基于gradle构建java应用程序示例
 

目录

你需要什么
用户手册
初始项目
项目结构
编译项目
运行项目
总结
源码
 

你需要什么

  • 大约8分钟
  • 一个文本编辑器
  • 命令行终端
  • jdk1.7或更高版本
  • 最近版本的gradle

用户手册

gradle带有内置的init插件。它的文档地址
这个插件有一个task,名叫init,可以生成项目. init task使用内置的wrapper task生成wrapper脚本gradlew
 
运行init task
gradle init --type <name>
参数name可选值
  • java-application
  • java-library
  • scala-library
  • groovy-library
  • basic
这里我们选择java-application类型
 
首先创建项目目录
$ mkdir java-demo
$ cd java-demo
 

初始化项目

在项目目录中运行init task,并使用参数java-application
$ gradle init --type java-application
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :wrapper
> Task :init

BUILD SUCCESSFUL
 
init task先运行wrapper task生成gradlew、gradlew.bat脚本,并创建下面的项目结构
├── build.gradle
├── gradle            #wrapper的文件
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │   └── java        #源文件
    │       └── App.java
    └── test            #测试
        └── java
            └── AppTest.java
 
 

项目结构

settings.gradle有很多注释,有效行只有一行
settings.gradle
rootProject.name='java-demo'
配置root project的名称java-demo
 
生成的build.gradle同样有很多注释,下面是有效部分
build.gradle
apply plugin: 'java'
apply plugin: 'application'

repositories {
    jcenter()      #公有仓库
}

dependencies {
    compile 'com.google.guava:guava:21.0'   #google的guava包
    testCompile 'junit:junit:4.12'          #junit测试包
}

mainClassName = 'App'    #main类
build文件中加了java和application两个插件。前者支持java项目,后者允许指定包含main方法的类,在编译时执行
 
src/main/App.java
public class App {
    public String getGreeting() {
        return "Hello world.";
    }

    public static void main(String[] args) {   #在运行application插件的run task时调用
        System.out.println(new App().getGreeting());
    }
}
 
src/test/java/AppTest.java
import org.junit.Test;
import static org.junit.Assert.*;

public class AppTest {
    @Test public void testAppHasAGreeting() {
        App classUnderTest = new App();
        assertNotNull("app should have a greeting",
            classUnderTest.getGreeting());
    }
}
 
编译项目
编译项目使用build命令。可以使用常用的gradle命令,但是使用wrapper脚本更好
$ ./gradlew build
> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :jar
> Task :startScripts
> Task :distTar
> Task :distZip
> Task :assemble
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses

> Task :test

> Task :check
> Task :build

BUILD SUCCESSFUL
注意:第一次运行时会比较慢
 
第一次运行build时,会检查.gradle目录是否存在guava、junit包。如果不存在,则下载并存到本地目录
下次运行build就使用缓存。build task编译class文件,运行test用例,生成测试报告
 
查看html报告build/reports/tests/test/index.html.
样例
 

运行项目

gradle构建使用了application插件,所以能在命令行运行程序。
看看有哪些task
$ ./gradlew tasks
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

// ... many other tasks ...
 
run task会让gradle读取mainClassName属性指定的类,执行main方法
$ ./gradlew run
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:run
Hello world.


BUILD SUCCESSFUL
 

总结

通过上面介绍可以学到
  • 如何生成java应用程序
  • 如何生成build文件、java文件
  • 如何运行构建、查看测试报告
  • 如何使用application插件的run task执行程序

源码

github地址
 
参考资料
 
了凡四训》详解之改过之法
印光大师十念法(胡小林主讲第1集)

 

Avatar_small
CSPM for SaaS 说:
2020年12月14日 15:04

New web site is looking good. Thanks for the great effort.

Avatar_small
SEO 说:
2023年8月24日 19:43

You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant! https://boltposts.com/dewapoker/

Avatar_small
Johny 说:
2023年8月27日 11:04

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon. rose vibrator

Avatar_small
SEO 说:
2023年8月28日 16:48

This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!. พนันออนไลน์

Avatar_small
SEO 说:
2023年8月29日 18:47

I was surfing the Internet for information and came across your blog. I am impressed by the information you have on this blog. It shows how well you understand this subject. IDN Poker

Avatar_small
SEO 说:
2023年8月30日 17:56

Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people.. kangtoto

Avatar_small
qk 说:
2023年9月09日 22:45

This was really an interesting topic and I kinda agree with what you have mentioned here! plastic surgeon

Avatar_small
jnanabhumiap.in 说:
2024年1月08日 18:02

JNANABHUMI AP provides all latest educational updates and many more. The main concept or our aim behind this website has been the will to provide resources full information on each topic which can be accessed through Internet. To ensure that every readers get's what important and worthy about the topic they search and link to hear from us. jnanabhumiap.in Jnanabhumi AP is a startup by passionate webmasters and bloggers who have passion to provide engaging content which is accurate, interesting and worthy to read. We are mope like a web community where you can find different information's, resources, topics on day to day incidents or news. We provide you the finest of web content on each and every topics possible with help of editorial and content team.

Avatar_small
gptnederlands 说:
2024年4月14日 16:35

The article examines the capabilities of ChatGPT, the AI language model, and its potential impact on the software development landscape. It delves into how ChatGPT's advanced natural language processing abilities could automate certain coding tasks, streamline the debugging process, and even assist in generating creative solutions to complex programming challenges. The author offers a thought-provoking exploration of the future intersection of AI and software engineering.

Avatar_small
rahul 说:
2024年4月20日 13:31

Hello, Thanks for sharing such a useful information about Java. You can visit here for more information, <a href="https://www.sevenmentor.com/java-course-in-solapur">Java Course in Solapur</a> .

Avatar_small
rahul 说:
2024年4月20日 13:36

Hello, Thanks for sharing such a useful information about Java. You can visit here for more information, https://www.sevenmentor.com/data-analytics-courses-in-pune.php


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter