Gradle 8.6
I am trying to sign jars using jarsigner from Gradle task.
- The first thing I did was generate a certificate:
keytool -genkey -alias test -validity 3650 -keyalg RSA -keystore test.jks
After entering the command, I set a password and specified all the necessary data.
- I put the certificate in the Gradle root folder:
C:\Users\User\.gradle\test.jks - I also created and filled in the global
gradle.propertiesfile:
org.gradle.warning.mode=all
testKeyStore=C:/Users/User/.gradle/test.jks
testKeyStoreAlias=test
testKeyStorePassword=test
testKeyStoreKeyPassword=test
tsa=http://time.certum.pl
- Next, I created a Gradle Task:
tasks.register("signJars") {
onlyIf { // Skip the task if our secret data isn't available
project.hasProperty("testKeyStore") &&
project.hasProperty("testKeyStoreAlias") &&
project.hasProperty("testKeyStorePassword") &&
project.hasProperty("testKeyStoreKeyPassword") &&
project.hasProperty("tsa")
}
final def testKeyStore = findProperty("testKeyStore")
final def testKeyStoreAlias = findProperty("testKeyStoreAlias")
final def testKeyStorePassword = findProperty("testKeyStorePassword")
final def testKeyStoreKeyPassword = findProperty("testKeyStoreKeyPassword")
final def tsa = findProperty("tsa")
for (jar in fileTree(dir: "$buildDir/libs", include: '*.jar')) {
final List<String> args = Arrays.asList(
"jarsigner",
"-keystore",
"\"${testKeyStore}\"",
"-storepass",
"\"${testKeyStorePassword}\"",
"-keypass",
"\"${testKeyStoreKeyPassword}\"",
"-tsa",
"\"${tsa}\"",
"-verbose",
"\"${jar.toPath()}\"",
"\"${testKeyStoreAlias}\""
) as List<String>
final command = args.join(" ");
println command
def builder = new ProcessBuilder(command)
builder.inheritIO()
def process = builder.start()
def finished = process.waitFor(15, TimeUnit.SECONDS)
println "Finished: ${finished}"
println "Exit value: ${process.exitValue()}"
println "Output: ${process.text}"
}
}
After Gradle Reload, I tried to run the task, to which I received an answer:
Cannot run program "jarsigner -keystore "C:\Users\User\.gradle\test.jks" -storepass "test" -keypass "test" -tsa "http://time.certum.pl" -verbose "E:\full\path\to\project\Project\build\libs\Test.jar" "test"": CreateProcess error=2, The system cannot find the file specified
I've tried many different variations, but I've never been able to make it work. I need help!
The most interesting thing is, if I copy the command and execute it from Windows PowerShell or IDEA, then everything works WITHOUT problems!
I was guided by the following materials when trying to implement this task: stackoverflow и gist.github
Thanks to @Robert answer, I again drew attention to the ant
signjartask, this time I managed to solve the problems that arose. https://ant.apache.org/manual/Tasks/signjar.html