Upload war but not jar

In a multi-module build, some projects create jars but some only wars. To avoid uploading an empty jar from my war module, I tried applying the java-base plugin rather than ‘java’. The result, even though the maven and maven-publish plugins are still applied, is that the install task appears to vanish. A call to ‘gradle install’ on project ‘xxx’ fails with the message 'Task ‘install’ not found in project ‘:xxx’. What is going on, and how do I fix it? Thank you for your help.

If you’re trying to get a project to only make a war, apply the ‘war’ plugin.

This will automatically disable the jar task and create a war task instead.

This also applies the Java plugin by default.

Word for the wise though: the default configuration of a project of the ‘war’ plugin has the jar assigned as an artifact, at least in the version I was using. Use the archives configuration instead

Interesting. I ended up disabling the jar task with jar.enabled = false. How exactly do you ‘use the archives configuration instead of the default configuration’ for the war plugin?

Depends.

If you have your ‘war’ project as a dependency, you can define the configuration where you define the dependency.

dependencies {
    compile project(path:':path:to:project', configuration: 'archives'
}

From this: https://docs.gradle.org/current/userguide/maven_plugin.html:

Installs the associated artifacts to the local Maven cache, including Maven metadata generation. By default the install task is associated with the archives configuration. This configuration has by default only the default jar as an element.

Looks like install automatically chooses what’s in the ‘archives’ configuration, so it should upload just your war, assuming you have the war plugin applied.

Now that is a useful move for the project that absorbs and packages the war, no doubt, thank you. My original question, though, concerned the project that builds the war. That one seems to need jar.enabled=false to prevent it from packaging and uploading an empty jar file.

I will say that I don’t have a lot of experience with the Maven plugin. Is it possible for you to share your build.gradle file?

It’s just a build file that collects jars from other projects in the build, does some work on auxiliary files, and packages them into the war. The problem is that 1. the war plugin has expectations that the java plugin is present - it uses the runtime() configuration, for example -
2. the java plugin adds ‘jar’ to the list of artifacts to install and publish
3. so the code installs/publishes an empty jar file (yuck)

I can prevent the publishing to artifactory by setting jar.enabled=false, but then the install task fails.

There has to be a way to remove ‘jar’ from gradle-maven’s list of artifacts to install.

ah-ha! With some help from my colleagues, the answer is now found. It doesn’t work unless some other artifact has been added to install, but on that condition it does work:

install {
doFirst{
Configuration archivesConfig = project.getConfigurations().getByName(‘archives’)
archivesConfig.artifacts.removeAll {it.extension.equals(‘jar’)}
}
}

Glad you figured it out. There are a lot of ways to do things between the Groovy language and the Gradle syntax, so it can get confusing sometimes. Best of luck to you!