简体   繁体   English

创建Eclipse Java Gradle项目

[英]Create Eclipse Java Gradle Project

I would like to use Eclipse with Gradle to build POJO's. 我想将Eclipse与Gradle结合使用来构建POJO。 I installed the Eclipse STS plugin as highlighted in the Gradle tutorial. 我安装了Gradle教程中突出显示的Eclipse STS插件。 I create Gradle project with Eclipse using File > New > Gradle(STS) > Gradle(STS) Project then type in the project name and select Java Quickstart. 我使用Eclipse使用File> New> Gradle(STS)> Gradle(STS)Project创建Gradle项目,然后输入项目名称并选择Java Quickstart。 It sets up the project correctly but includes an annoying "org.gradle" Package and Person.java classes throughout the main java/resources and test java/resources. 它正确设置了项目,但在整个主要java / resources中包含烦人的“ org.gradle” Package和Person.java类,并测试了java / resources。 I go in and delete them and create my own com.xxx package and classes. 我进入并删除它们,并创建自己的com.xxx包和类。 Anyway that I an suppress the creation of these files? 无论如何,我禁止创建这些文件?

I investigated several methods highlighted by Gradle's Tutorial website and a few other places and found a better way of setting up a Java project utilizing Gradle. 我研究了Gradle的Tutorial网站和其他一些地方强调的几种方法,并找到了一种更好的利用Gradle设置Java项目的方法。 Here is what I did. 这是我所做的。

  1. Using Eclipse, create a Java Project. 使用Eclipse创建一个Java项目。 file > New > Java Project -- Add Project Name & click finish. 文件>新建> Java项目-添加项目名称并单击完成。

  2. Delete the src file and create a new file hierarchy file > New > Source Folder -- Type src/main/java file > New > Package -- com.mycompany. 删除src文件并创建一个新的文件层次结构文件>新建>源文件夹-键入src / main / java文件>新建>包-com.mycompany。

    Do the same for Resources and Test if required src/main/resources + Package src/test/Java + Package 对资源执行相同的操作,并在需要时进行测试src / main / resources + Package src / test / Java + Package

  3. Add/create your classes and Jars to the project. 向项目添加/创建您的类和Jar。

  4. Create build.gradle file in project root. 在项目根目录中创建build.gradle文件。 Add this simple Gradle project build script, 添加这个简单的Gradle项目构建脚本,

    apply plugin: 'java' apply plugin: 'application' Apply插件:“ java” Apply插件:“ application”

    mainClassName = 'com.mycompany.HelloWorld' mainClassName ='com.mycompany.HelloWorld'

    repositories { mavenCentral() } 存储库{mavenCentral()}

    jar { baseName = 'myjarname' version = 'v1' manifest { attributes 'Main-Class': 'com.mycompany.myproject' } doFirst { from { configurations.runtime.collect { it.isDirectory() ? jar {baseName ='myjarname'version ='v1'manifest {属性'Main-Class':'com.mycompany.myproject'} doFirst {来自{configuration.runtime.collect {it.isDirectory()? it : zipTree(it) } } } } 它:zipTree(it)}}}}

    sourceCompatibility = 1.8 targetCompatibility = 1.8 sourceCompatibility = 1.8 targetCompatibility = 1.8

    dependencies { compile 'joda-time:joda-time:2.2' testCompile 'junit:junit:4.12' } 依赖项{编译'joda-time:joda-time:2.2'testCompile'junit:junit:4.12'}

You can get the dependency scripts from Maven Central (search.maven.org). 您可以从Maven Central(search.maven.org)获得依赖脚本。 Note that this gradle script will allow you to build a Fat Jar -- a project jar with multiple dependency jars. 请注意,此gradle脚本将允许您构建Fat Jar-一个具有多个依赖项jar的项目jar。

  1. Next create the Gradle wrapper. 接下来创建Gradle包装器。 Open a command prompt and go to the project root directory. 打开命令提示符,然后转到项目根目录。 Type -- gradle wrapper. 类型-gradle包装器。 All of the correct wrapper files are now created for your project. 现在为您的项目创建了所有正确的包装文件。

  2. While in the project root type -- gradlew build. 在项目根目录下-gradlew构建。 This create other files and builds the project. 这将创建其他文件并构建项目。 You can go to project root\\build\\libs and you will find your executable Jar. 您可以转到项目root \\ build \\ libs,然后找到可执行的Jar。

  3. As you make changes to your project from now on you simply go to the project root in command prompt and type -- gradle build. 从现在开始对项目进行更改时,只需在命令提示符下转到项目根目录,然后键入-gradle build。

I would call this a starter gradle build for newbies. 我将其称为新手入门入门构建。 However, it will get you going and as you need more gradle capability you can modify the build.gradle. 但是,它可以助您一臂之力,并且当您需要更多的gradle功能时,可以修改build.gradle。 Also, don't forget, as you add new jar libraries you will need to update the build.gradle dependencies. 另外,不要忘记,在添加新的jar库时,您将需要更新build.gradle依赖项。 This process also works with Eclipse so you can directly build the project from eclipse and run it in the console. 此过程也适用于Eclipse,因此您可以直接从eclipse构建项目并在控制台中运行它。 In this example I was using Gradle 3.0 and Java SE 1.8. 在此示例中,我使用了Gradle 3.0和Java SE 1.8。

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

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