简体   繁体   English

配置现有的eclipse java项目使用gradle构建

[英]configuring existing eclipse java project to build using gradle

I have an existing Java project in Eclipse. 我在Eclipse中有一个现有的Java项目。 I want to implement builds using gradle. 我想使用gradle实现构建。 I tried using the gradle eclipse plugin as given here but ran into numerous errors in Eclipse. 我尝试使用这里给出的gradle eclipse插件,但在Eclipse中遇到了很多错误。

I am using gradle 1.3, and I tried running gradle from command prompt, but I get compilation errors. 我正在使用gradle 1.3,我尝试从命令提示符运行gradle,但是我收到了编译错误。

So my question is, does anyone know of some good resource which offers a how-to for converting an existing java project in Eclipse to compile using gradle. 所以我的问题是,有没有人知道一些好的资源,它提供了一个如何在Eclipse中转换现有的java项目以使用gradle进行编译。 I also have some dependencies on other projects. 我也对其他项目有一些依赖。 The link to the tutorial I have given is not really helpful. 我给出的教程链接并没有多大帮助。

UPDATE: I can get gradle to work if my project doesn't have dependencies on other projects. 更新:如果我的项目没有依赖于其他项目,我可以让gradle工作。 However, if it does refer to some other project, I can't figure out how to reference another project? 但是,如果它确实引用了其他一些项目,我无法弄清楚如何引用另一个项目? I have added the referenced project dir to repositories but I still get "class does not exist" errors. 我已将引用的项目目录添加到存储库但我仍然得到“类不存在”错误。 My gradle file is as below: 我的gradle文件如下:

apply plugin: 'java'
apply plugin: 'eclipse'

version='1.0-SNAPSHOT'

def repositoryPath = 'C:/Users/AMoody/workspace/temp/temp-protocols/'

repositories{
    mavenCentral()
    flatDir {
        dirs repositoryPath
    }
}

dependencies{

    compile 'org.slf4j:slf4j-api:1.+'
    compile 'junit:junit:4.+'
     testCompile 'junit:junit:4.+'

}

Okay, So if you have simple java project which has dir/file structure as below 好的,所以如果你有简单的java项目,其dir /文件结构如下所示

在此输入图像描述

Step-1: create file name build.gradle in project root directory as below 步骤1:在项目根目录中创建文件名build.gradle ,如下所示

在此输入图像描述

Step-2: add following gradle script in build.gradle 步骤2:build.gradle添加以下gradle脚本

apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'someJar'
version = '1.0-SNAPSHOT' 

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes 'Main-Class': 'com.test.Run'
    }
}

dependencies {
   compile  'log4j:log4j:1.2.16'
} 

Step-3: close or delete from eclipse this project and import it again while selecting Gradle project as below 步骤3:关闭或删除eclipse这个项目,并在选择Gradle项目时再次导入它,如下所示

在此输入图像描述

在此输入图像描述

Now your project hierarchy will look like this. 现在,您的项目层次结构将如下所示。 (Also note its added Gradle Dependencies while importing project) (还要注意导入项目时添加的Gradle依赖项)

在此输入图像描述

Step-4: Create source foler src/main/java in project and move your all packages in that source folder. 步骤4:在项目中创建源代码src/main/java并移动该源文件夹中的所有包。

在此输入图像描述

Step-5 last but not the least: Cheers :) 步骤5最后但并非最不重要: 干杯:)

So now your Simple Java project has converted into Gradle project! 所以现在你的Simple Java项目已经转换为Gradle项目了!

The first couple of chapters of the Gradle User Guide , along with the many (Java) samples in the full Gradle distribution , should get you started. Gradle用户指南的前几章以及完整Gradle发行版中的许多(Java)示例应该可以帮助您入门。 It doesn't matter all that much whether your current build is Eclipse or something else; 无论您当前的构建是Eclipse还是其他东西,都无关紧要; to be successful with Gradle, you'll first have to master its fundamentals. 要想成功使用Gradle,您首先必须掌握其基础知识。

Once you have the Gradle command line build working, you can give the IDE integration another shot. 一旦Gradle命令行构建工作,您可以为IDE集成另一个镜头。 Depending on your preferences, you can either generate IDE files with Gradle's Eclipse plugin (again see the samples in the full Gradle distribution), or use the Eclipse Gradle Integration for a more integrated IDE experience. 根据您的偏好,您可以使用Gradle的Eclipse插件生成IDE文件(再次参阅完整的Gradle分发中的示例),或使用Eclipse Gradle Integration获得更加集成的IDE体验。

Check out the Gradle homepage for additional resources. 查看Gradle主页以获取更多资源。 When faced with a concrete question about Gradle's build language, consult the Gradle DSL Reference . 当遇到有关Gradle构建语言的具体问题时,请参阅Gradle DSL参考 A full-text search over the single-page Gradle User Guide can also be quite effective. 通过单页Gradle用户指南进行全文搜索也非常有效。 Last but not least, make sure to visit the Gradle Forums . 最后但同样重要的是,请务必访问Gradle论坛

尝试使用Gradle的Eclipse集成: http//marketplace.eclipse.org/content/buildship-gradle-integration

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

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