How to prevent creation of jar task's default artifact

I learned how to add new tasks for creating additional jars, but could not figure out how to prevent creation of the default artifact of jar task. I found project.getTasks gives the TaskContainer, but no remove method in it. Replace could be used as a workaround, but I do not like this idea. There must be a better way.

Is there?

Thanks!

Although it removes the jar task, still has no effect to the outcome:

afterEvaluate { project -> …

iter = project.getTasks().iterator()
while (iter.hasNext()) {
   task = iter.next()
   if (task.name == 'jar') {
      println task.name + ' removed'
      iter.remove()
  }
}

OK, found two ways:

  1. jar.onlyIf { condition…} 2. jar.enabled = condition…

You already found the two ways to disable a task. In the upcoming milestone 5, a task can also be removed (at configuration time) with “project.tasks.remove(mytask)”.

Thanks for the info and following the topic!

The tasks.remove in M5 and above still leaves me a bit confused. In M6 given the following build.gradle:

apply plugin: 'java'
project.tasks.remove(jar)

We get the following result:

nadurra:javaplugin mbjarland$ gradle jar
  FAILURE: Could not determine which tasks to execute.
  * What went wrong:
Task 'jar' not found in root project 'javaplugin'.
  nadurra:javaplugin mbjarland$ gradle build
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE
  BUILD SUCCESSFUL

In other words, the ‘jar’ task is removed from the user-invokable list of tasks, but it is still invoked when running ‘build’.

Also as per my earlier post on the jar task, it creates directories/etc even when it is enabled=false or onlyIf { false }.

Mattias, try this:

configurations.runtime.with {
  artifacts.remove artifacts.find { it.archiveTask.is jar }
}

With 1.0-milestone-6 (because it uses Groovy 1.8) you can do this…

configurations.runtime.artifacts.removeAll { it.archiveTask.is jar }

The ‘build’ task builds all the artifacts in a project. Disabling the jar task doesn’t remove it from the artifacts.

1 Like

Hello,

I am also looking at how to bypass the use of the default jar.

Luke’s above snippets work; the artifact is not passed on to dependent projects which was my main problem.

Why is in the the runtime configuration the above statement should be applied? Using it on the default or compile configuration does not work.

Thank you, Stefan

I attempted this to remove the default jar so I could use an obfuscated jar in it’s place:

configurations.runtime.artifacts.removeAll { it.archiveTask.is jar }

but got:

You can’t change a configuration which is not in unresolved state!

Ping!

I really need a solution to replacing the default artifact with an obfuscated version when the obfuscate task runs. It has been quite frustrating thus far.

I have managed to get it to work when publish in. But when I have a multi-project build it is always the unobfuscated file that is passed on to dependent projects. Even though I replace the archive artifacts such that the obfuscated version is what gets published.

What is the right way to replace the default artifact?

I’d like to bump this topic up. I’m also very much interested in a solution.

Like swpalmer, I’d like to overwrite the default artifact with a post-processed jar file at a different location created by a different task. Is there a solution to this?

I found the solution here: http://forums.gradle.org/gradle/topics/how_do_you_change_the_default_artifact_in_a_multi_project_build