Looking to create task which configures an eclipse project

I’ve played around a bit with generating eclipse projects (.project, .classpath, etc). I can get it to work by just using the eclipse {} object in my build.gradle. What I would like to do is create a configuration task which creates an eclipse model. This task would be able to take parameters, so in my main logic I can call it multiple times and generate multiple different projects.

I tried using a similar concept to a Zip type task, but can’t seem to get it to work. Anyone have any ideas?

apply plugin: 'java'
apply plugin: 'eclipse'
  task generateEclipseProject(Type: GenerateEclipseProject, dependsOn: cleanEclipse) {
    project {}
    classpath {}
}
  task generateEclipseProject(Type: GenerateEclipseProject, dependsOn: cleanEclipse) {
   eclipse {
          project {}
      classpath {}
   }
}
  task generateEclipseProject(Type: EclipseModel, dependsOn: cleanEclipse) {
    project {}
    classpath {}
}
  task generateEclipseProject(Type: EclipseModel, dependsOn: cleanEclipse) {
   eclipse {
          project {}
      classpath {}
   }
}

The code snippet you posted has several problems, but in short, a task cannot generate a model (at least not today). A model needs to be configured in the configuration phase; a task runs in the execution phase (i.e. later). You can write code that generates a model, but it can’t be a task.

So how would I write code that generates a model? Would I just write a function?

def generateEclipseProject(String projectName) {
   return eclipse {
     project {}
     classpath {}
}

Maybe some background on what I’m trying to accomplish would help.

I’m trying to set up a way to easily generate an entire multi-project in 1 step. The user will set up a “master” directory and will have a simple build.gradle script. When they execute the task I’m trying to create, gradle will ask the user some questions (how many projects are trying to be created, what the name of each project is, what type of artifact is being generated for each project [jar|war|ear], etc). Once that information is gatthered, the script will go out and do the following:

  • Create a settings.gradle file in the current “master” directory - inside it will have an includeFlat with the names of the projects that were inputted. - Create a sibling directory for each project name that was inputted - Create an eclipse .project & .classpath file for each project. The name attribute in the .project file will correspond to the project name. All of the other information in the .project & .classpath files are static. - Create a gradle.properties file in each sibling directory for each project. There will be some properties in there that are specific to the project (via answers to questions that were asked). There will be some static properties that apply to all gradle.properties files that were created.

Is this something I can do using the eclipse plugin, or should I be looking more down the path of creating custom Java/Groovy classes and having some tokenized .project & gradle.properties files handy on the filesystem & just do token replacement as I copy them into each sibling directory?

Actually, in some cases it may be possible for a task to generate a model if the task runs early enough. I wouldn’t want to rely on this though.

What you are trying to do here seems unrelated to the Eclipse plugin, except that you also want to generate Eclipse files. If you do want to use the Eclipse plugin for that, a natural solution would be to make it a two-step process: Your task creates the build files (which include configuration of the Eclipse plugin), and after that the user runs ‘gradle eclipse’ to generate the Eclipse files.

Ok. I appreciate the help! I will look into this more.