Stumped on 'buildsrc'

(William Zeitler) #1

I’m an old hand at c++ and nmake, but a newbie to gradle, trying to implement building my current java project. For that I need to build a java build tool to generate java files for the main application. My build tool has an external dependency.

For my minimal example I have this folder structure:

   + buildSrc
      + buildTool

My build.gradle in the root looks like this:

apply plugin: 'java'
repositories { 

My build.gradle in ./buildSrc looks like this:

dependencies {
    compile "commons-cli", name: "commons-cli", version: "1.3"

My demo looks like this:

import org.apache.commons.cli.*;

public class buildTool {
    public static void main(String[] args) throws IOException {
		try {
			CommandLineParser parser = new DefaultParser();
		} catch (ParseException e) {

I’m using gradle 2.14.1 (though I get the same result with 3.0) and get:

Build file '/home/william/Documents/prj/sw/atarasec/grendel/code/buildSrc/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating root project 'buildSrc'.
> Could not find method apply() for arguments [{plugin=java}] on root project 'buildSrc' of type org.gradle.api.Project.

I’ve tried various permutations on the above source, but clearly I am missing something fundamental.

(James Justinic) #2

There are several issues with the minimal example, all of which are easily resolvable, but none of which should cause the error that you mentioned. If I fix these two issues, the minimal example provided runs correctly for me:

  • The buildSrc build.gradle needs a repositories declaration in order to resolve commons-cli.
  • The dependency declaration using map notation is missing the key for the first value (group).

Also, the error states that there is an issue with line 1 in buildSrc/build.gradle, but the message seems to reflect the contents of the root project build.gradle. Are you sure that this minimal example fails with the same error as your original project and that you accurately represented it here? It seems like something might be transposed. If you can’t tell what might different, pushing your minimal example exactly as you have it to GitHub might help.

(Stefan Oehme) #3

Please keep in mind that the buildSrc project is a completely separate build. Nothing you do in your main build.gradle will have any effect on the buildSrc project.

(William Zeitler) #4

Your suggestions plus fixing one other issue solved the problem.

The other issue was that somewhere along the line my build.gradle got saved as UTF8. So gradle was hitting the Byte Order Marker and failing. Saving build.gradle as ANSI fixed that!

(William Zeitler) #5

Thanks. I’m still a little fuzzy when gradle ‘inherits’ build.gradle’s higher in the file tree. If I understand aright, if I were to run gradle within ./buildSrc, then it would ‘inherit’ the build.gradle in the root? (But of course that’s not what happens when you run gradle in the root, and gradle finds the magic folder ./buildSrc and does its thing.)

(Stefan Oehme) #6

Would do you mean by inherit? As a I said, buildSrc is a completely separate build that is built before your main build.

What you probably mean is subprojects finding their root project. buildSrc is not a subproject.