如何将java源添加到gradle buildscript类路径中? [英] How to add java source onto gradle buildscript classpath?

查看:501
本文介绍了如何将java源添加到gradle buildscript类路径中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  buildscript {
依赖关系

我有一个Java类,我想从我的gradle构建脚本中调用: {
//这是我找不到的部分...
classpath文件(src / main / java / com / example)
}
}

import com.example.MyClass

任务运行器(dependsOn:'classes')<< {
def text = com.example.MyClass.doIt()
println text
}

doIt()方法只返回一个String。



项目布局看起来像:

 
├──build.gradle
└──src
└──main
└──java
└──com
└──示例
└──MyClass.java

我明白我需要将类添加到构建脚本依赖项,否则导入无效,但我不明白如何将项目源添加为构建脚本的依赖项。我已经尝试了以下方法:

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'测试)
classpath文件(src / main / java / com / example)

我无法绕过

 >启动失败:
构建文件'/Users/jameselsey/development/james-projects/gradle-test/build.gradle':8:无法解析类com.example.MyClass
@ line 8,column 1.
import com.example.MyClass
^

1错误

如何在构建脚本依赖项中添加 MyClass

解决方案您可以通过两种方式实现这一点。


  1. 如果您的Java代码很小,那么您可以简单地将其打开如果你的代码很大并且需要多个类,首选的选项就是使用隐式的[buildSrc]
    您可以在buildSrc / src / main / java下创建类,这些类将被编译并放置在buildscript类路径中,并可在gradle脚本中使用。

项目源(类,测试类,资源)在构建脚本中不可用。由于这会引入循环依赖,因为您需要构建来编译源代码,但构建需要该类来执行。



Gradle Documentation: http://gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources



示例: https://zeroturnaround.com/rebellabs/using-buildsrc-for-custom-logic-in-gradle-builds/


I have a Java class that I want to invoke from my gradle build script:

buildscript {
    dependencies {
        // This is the part I can't figure out...
        classpath files("src/main/java/com/example")
    }
}

import com.example.MyClass

task runner(dependsOn: 'classes') << {
        def text = com.example.MyClass.doIt()
        println text
}

The doIt() method just returns a String.

Project layout looks like:

.
├── build.gradle
└── src
    └── main
        └── java
            └── com
                └── example
                    └── MyClass.java

I understand that I need to have the class added to the build script dependency, otherwise the import is not valid, but I don't understand how I can add the project source as a dependency for the build script. I've tried the following:

classpath project
classpath rootProject
classpath project(":gradle-test")
classpath files("src/main/java/com/example")

I can't get around

> startup failed:
  build file '/Users/jameselsey/development/james-projects/gradle-test/build.gradle': 8: unable to resolve class com.example.MyClass
   @ line 8, column 1.
     import com.example.MyClass
     ^

  1 error

How do I add MyClass to the build script dependency?

解决方案

You could achieve this using two ways.

  1. If your Java code is small then you could simply turn it into groovy and include it in the gradle script.

  2. If your code is rather large and requires multiple classes, preferred option would be to use the implicit [buildSrc] You can create classes under buildSrc/src/main/java and those classes will get compiled and placed in the buildscript classpath and will be available to use during gradle script.

The project sources (class,test classes, resources) are not available during the build script. As this would introduce a cyclic dependency, since you need your build to compile the source, yet the build needs the class to execute.

Gradle Documentation: http://gradle.org/docs/current/userguide/organizing_build_logic.html#sec:build_sources

Example: https://zeroturnaround.com/rebellabs/using-buildsrc-for-custom-logic-in-gradle-builds/

这篇关于如何将java源添加到gradle buildscript类路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆