gradle javaexec error "'apiElements' directly is not allowed"- Gradle 5.4.1

huangapple go评论70阅读模式
英文:

gradle javaexec error "'apiElements' directly is not allowed"- Gradle 5.4.1

问题

以下是翻译好的部分:

我是Gradle的新手,正在尝试将现有的系统构建从Ant迁移到Gradle。
作为此过程的一部分,我需要在目录中的每个文件上运行一个Java程序。目录包含XML文件,Java代码将解析和转换 .xml 文件(在执行某些特定业务转换后,这些Java文件将被构建以生成最终jar中的类和包)。

以下是我在Gradle中编写的一个函数:

private runJavaFile(String dirPath) {
    FileTree tree = fileTree(dir: dirPath, include: '**/*.xml')
    tree.each {
        def xmlfile = it.path
        def javaFile = it.path.replaceFirst(".xml", ".java")
        javaexec {  // 在这一行报错
            classpath configurations.all
            main = 'XmlToJavaParser'
            args = ["$xmlfile", "$javaFile", 'Java']
        }
    }
}

我通过将包含要解析的xml文件的目录路径传递给Gradle任务来调用此函数。
在运行任务时,我遇到以下错误:

> Resolving configuration 'apiElements' directly is not allowed

将不允许直接解析配置“apiElements”。

如果需要更多信息,请告诉我。

英文:

I am new to Gradle and trying to migrate an existing system build from ant to Gradle.
As part of this I need to run a java program on every file in a directory. Directory contains xml files and the java code will parse and convert .xml to .java files (and these Java files would be build to generate class and package in final jar) after performing some business specific transformation.

below is a function I wrote in Gradle

private runJavaFile(String dirPath) {
    FileTree tree = fileTree(dir: dirPath, include: '**/*.xml')
    tree.each {
        def xmlfile = it.path
        def javaFile = it.path.replaceFirst(".xml", ".java")
        javaexec {  //// getting error on this line
            classpath configurations.all
            main = 'XmlToJavaParser'
            args = ["$xmlfile", "$javaFile", 'Java']
        }
    }
}

I am calling this function from a Gradle task by passing the dir path which contains the xml files to be parsed.
While running the task, I am getting below error:

> Resolving configuration 'apiElements' directly is not allowed

Any help would be appreciated.
Let me know if any more information is needed.

答案1

得分: 1

在Gradle中,一个配置代表了一组构件及其依赖关系。通常情况下,您会有多个配置,具体取决于您的需求。例如,您可以声明一个配置,用于指定编译所需的依赖项,另一个用于仅在运行时需要的依赖项,或者用于运行特定Java应用程序所需的依赖项。

在您的情况下,您说XmlToJavaParser类的类路径是“所有配置组合”,这实际上没有意义。您也不被允许这样做,因为Java插件的某些配置无法像这样解析,这就是您出现错误的原因。

因此,要修复它,您应该为XmlToJavaParser声明自己的配置。然后,您可以像通常一样为它声明依赖关系。以下是一个示例(使用Groovy DSL):

configurations {
    xmlJavaParser {
        canBeResolved = true
        canBeConsumed = false
    }
}

dependencies {
    xmlJavaParser "org.example:xml-java-parser:1.0" // 或者您需要的任何依赖项
}

private runJavaFile(String dirPath) {
    // ...
    javaexec {
        classpath = configurations.xmlJavaParser // 此处引用了配置
        main = 'XmlToJavaParser'
        args = ["$xmlfile", "$javaFile", 'Java']
    }
}

还有其他方法可以解决这个问题。但主要的要点是不要将configurations.all用作类路径。

英文:

In Gradle, a configuration represents a group of artifacts and their dependencies. You typically have several configurations depending on what you want to do. For instance, you could have one where you declare which dependencies are needed for compilation, which are only needed at runtime, or which are needed for running a particular Java application.

In your case, you are saying that the classpath to the XmlToJavaParser class is "all configurations combined" and that doesn't really make sense. You are also not allowed to do that as some configurations from the Java plugin are not resolvable like this, which is why you get an error.

So to fix it, you should declare your own configuration for XmlToJavaParser. You can then declare dependencies for it like you normally do. Example (using the Groovy DSL):

configurations {
    xmlJavaParser {
        canBeResolved = true
        canBeConsumed = false
    }
}

dependencies {
    xmlJavaParser "org.example:xml-java-parser:1.0" // or whatever you need
}

private runJavaFile(String dirPath) {
    // ...
    javaexec {
        classpath = configurations.xmlJavaParser // The configuration is referenced here
        main = 'XmlToJavaParser'
        args = ["$xmlfile", "$javaFile", 'Java']
    }
}

There are also other ways to go about it. But the main point is to not use configurations.all as a classpath.

huangapple
  • 本文由 发表于 2020年8月5日 17:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63262531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定