For more background… I want to publish unobfuscated code to MavenLocal for easier development/debugging. I want to publish obfuscated code to the main repo.
(Note that the mavenLocal issues with not finding dependencies are becoming extremely painful.)
There is no best practice; the solution depends on the specifics of how the tasks/plugins (e.g. proguard) work. Looking at your solution above, I’d try to configure the correct archive from the start, rather than fixing it up later. To run conditional code depending on whether a certain task will get executed, you can use ‘gradle.taskGraph.whenReady { graph -> if (graph.hasTask(“foo”)) { … } }’.
With the ‘maven’ plugin, you might have no choice but to fix up the artifacts similar to what you did above. Something like ‘configurations.archives.artifacts.clear(); artifacts { archives … }’.
Thanks for the response. I thought that the whenReady mechanism would make sense, but I didn’t have a clear idea of what I am allowed to do at that point. I guess anything that doesn’t change the taskGraph?
So something like this?
gradle.taskGraph.whenReady { graph -> if (graph.hasTask(“obfuscate”)) {
Something like this should work, but you might additionally have to undo the fact that the Java plugin already does ‘artifacts { archives jar }’ (see my previous post). Configurations (and also ‘Configuration.artifacts’) are ‘Buildable’ and therefore task dependencies are inferred automatically if some task takes them as input.
Okay… so I do the whenReady stuff to replace the Jar with the obfuscated version, and that seems to work. But in my multi-project build I have a project that does some integration tests with JNI methods that depends on the output of my java project and a native project. It seems now that it doesn’t run the obfuscate step on the java project before trying to run those tests. That in turn leads to the main output artifact of that project not being present when it tries to compile the test code and so it fails.
It seems that when the task graph is ready it is too late to replace the artifact because the ‘test’ task in my IntegrationTest project thinks that the output of the ‘jar’ task is the artifact and that it doesn’t need to run the obfuscate step on my java library. By the time it runs though the artifact has been replaced with the obfucated jar… but that hasn’t been built yet because the task graph was already determined!
I have temporarily worked around this by making my integration tests explicitly declare the need to run the obfuscation step in the other project, like this: