How to generate source code during project import?

I’m a happy command line user and use the gradle eclipse tasks frequently. Now that Buildship is 2.0 I thought it’s time to give it a try, composite builds and workspace resolution of dependencies are the topics I’m really interested in and it looks great so far.

One thing that bugs me is the ability to generate source code during project import. I have this tiny build.gradle file for testing purposes:

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

def generatedResources = file("$buildDir/generated-resources/main")

sourceSets {
    main {
        resources {
            srcDir generatedResources
        }
    }
}

//a task that generates the resources:
task generateMyResources {
  doLast {
    def generated = new File(generatedResources, "myGeneratedResource.properties")
    generated.parentFile.mkdirs()   
    generated.text = "message=Stay happy!"
  }
}

eclipseClasspath.dependsOn generateMyResources

Doing a gradle eclipse on the command line correctly generates the files. In Eclipse the source folder is present as well. Good so far!

When I import the project into Eclipse via Buildship the generateMyResources task is never executed and consequently the source folder is not present. I know that the Eclipse Plugin and Buildship are separate things and of course, I can execute the generateMyResources task by hand and do a [Gradle] -> [Refresh Gradle Project] but that’s a manual step which is kind of unnecessary in my opinion.

Is there any way I can tell Buildship to have my generated sources included during project import?

There is currently no way to do this, but definitely something Gradle should support. If you’d like to contribute such a functionality, we would be happy to guide you through the process.

There is currently no way to do this, but definitely something Gradle should support.

If that’s the case, do you think it qualifies for a feature request in Bugzilla?

Sure, please go ahead if there isn’t already one covering this.

Filed under Bug 510331 in the Buildship issue tracker.

I created an account on https://bugs.eclipse.org/ to comment on the bug but all I got was “You are not permitted to edit bugs in product Buildship.” when trying to add a comment. I like Github issues more…
Any how, here is my comments:

We actually have two slightly different uses for such functionality.

  1. We copy eclipse project and jdt settings from templates to the all eclipse projects. This must be done before import. Maybe we could replace some of this by using model based configuration, but we are not there yet.

  2. We also have a lot of generated code. Making sure this is executed before import will enable a smoother import. Otherwise Eclipse will start building and fail miserably before the precompile tasks has been executed.

I would vote for some way to ensure that all Buildship imports will execute the tasks specified instead of having to configure the tasks every time the project is imported into Eclipse.
Something syntax like:

eclipse {
  importDependsOn setupIde, precompile
}

Solving this would reduce the feature set difference in the STS migration guide at https://github.com/eclipse/buildship/wiki/Migration-guide-from-STS-Gradle-to-Buildship

I just added your requirement/proposal to the feature request.

OK, thanks. Maybe there is a grace period for commenting to reduce SPAM.

Eclipse is currently manually approving all new accounts and the warning message is very easy to miss. Thanks for going through all the trouble to give us your feedback, this is definitely they way we’d want to implement it.

To conclude this topic, automatic task execution is available for quite some time now. It first arrived with Eclipse Buildship 3.1 and Gradle 5.4 and works like a charm.

plugins {
    id 'eclipse'
}

task generateCustomConfig {
    doLast {
          println "Generating custom configuration..."
    }
}

eclipse {
    synchronizationTasks generateCustomConfig
}

More information in the blog post Automatic task execution in Buildship and Javadoc.