How to add task outputs to a web archive?

I’ve written a Gradle Task (in Java) that creates an @OutputFile.

@OutputFile

private final File output;

This output is written to “build/gen/module.xml” and I’d like it to be packaged in the web archive at the root.

myapp.war

|-- gen

|-- module.xml

How can I do this programmatically in the plugin?

You can do this:

war {
  from customTask
}

Thanks (as always) for the prompt response. How would I do this in Java inside my plugin code. So far I’ve got:

plugins.withType(WarPlugin.class, new Action() {

@Override

public void execute(WarPlugin plugin) {

WarPluginConvention convention = project.getConvention()

.findByType(WarPluginConvention.class);

// add the task outputs to the WarTask?

}

});

What exactly is producing the war? Is it the war task added by the ‘war’ plugin?

Yes, the WarPlugin that’s one of the core plugins.

Ok, something like…

plugins.withType(WarPlugin.class, new Action() {
  public void execute(WarPlugin plugin) {
    project.getTasks().getByName("war").from(myCustomTask)
  }
});

That’s assuming that ‘myCustomTask’ is an instance of your custom task and that you have a ‘final’ instance of the ‘Project’ object named ‘project’ in scope.

Thanks. It almost works the only problem is that the directory structure of the file isn’t being mirrored.

@OutputFile

private final File output = new File(project.getBuildDir(), “gen/module.xml”);

Inside the web archive I’m getting:

myapp.war

|-- module.xml

<< what I’m getting

|-- gen

|-- module.xml

<< what I’d like

The code I’ve got at the moment is:

plugins.withType(WarPlugin.class, new Action() {

@Override

public void execute(WarPlugin plugin) {

((War) tasks.getByName(“war”)).from(task);

}

});

You need to change your custom task to something like:

@OutputDirectory
File output = new File(project.buildDir, "gen")

Or duplicate the path name:

tasks.getByName("war")).from(task).into("gen");

Perfect, thanks Luke. I’m not sure how to mark this as solved but it’s all sorted.

Cheers.

No probs, glad it’s working for you.

I’ll close it off.