How to add task outputs to a web archive?


(Christopher Molozian) #1

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?


(Luke Daley) #2

You can do this:

war {
  from customTask
}

(Christopher Molozian) #3

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?

}

});


(Luke Daley) #4

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


(Christopher Molozian) #5

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


(Luke Daley) #6

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.


(Christopher Molozian) #7

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);

}

});


(Luke Daley) #8

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");

(Christopher Molozian) #9

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

Cheers.


(Luke Daley) #10

No probs, glad it’s working for you.

I’ll close it off.