Possible to create JAR artifact by defining Manifest in Gradle?

Answered

Hi, there are many error conditions that arise when building JAR artifacts, such as an "Invalid signature file digest" , "no main manifest attribute" and others. Often, a suggested solution is to add or edit a jar { } section in the build.gradle file. However in my experience, IntelliJ totally ignores the jar { } section, it *requires* the developer to enter a path to MANIFEST.MF and *only* uses that (although sometimes it overrides it with a MANIFEST.MF from inside a dependency).

Is it possible to insist that IntelliJ use the Gradle jar { } definition to create the MANIFEST.MF?

 

* I am building a Kotlin + Java application using IntelliJ 2019.3.1 Community, on Windows 10 Pro, with Gradle. I create a JAR artifacts using the "from modules with dependencies" option, where a path to META-INF\MANIFEST.MF is required (if it is left blank, IntelliJ seems to find a MANIFEST.MF from an extracted dependency package)

 

examples of solutions based on editing jar { } in build.gradle:

https://stackoverflow.com/a/14441628

https://stackoverflow.com/a/32567525

https://stackoverflow.com/a/9689877

 

0
4 comments
Avatar
Permanently deleted user

Learning about shadowJar (https://github.com/johnrengelman/shadow) and learning how to compile my gradle builds from the command line (pretty easy, "gradlew build") has freed me from the nightmare of building JARs with IntelliJ.

To Jetbrains: perhaps your tutorials work for "hello world" scenarios, but real world apps have multiple dependencies, require external resource files (like configs, env variables), may have security conflicts, mix Kotlin with Java classes, etc. etc. in which case tutorials like https://www.jetbrains.com/help/idea/packaging-a-module-into-a-jar-file.html and https://www.jetbrains.com/help/idea/working-with-artifacts.html and https://www.jetbrains.com/help/idea/gradle.html are more of a cruel joke than actual help.

0

Artifacts that you create manually via "From modules with dependencies" option are not synchronized with the Gradle configuration.

IDE auto-creates and auto-configures artifacts for the web application modules (the war Gradle plugin). It does not create them for the plain jar java module. So if you rely on Gradle for this configuration, you need to execute corresponding Gradle task to create such jar artifact and use Gradle runner for the Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using option.

0
Avatar
Permanently deleted user

I already had Gradle selected for both "Build and run using" and "Run tests using".

What finally worked, was opening the Gradle window (upper right), selecting the desired command (in my case, Tasks | Build | build or Tasks | shadow | shadowJar), right-clicking and selecting Execute After Build. Now when I use IntelliJ's Build | Build Project, the program compiles AND Gradle creates the JAR file.

If I was programming in any other language, I'd say, hey well IntelliJ isn't going to be perfect for Scala, or Dart, or whatever. Especially if the language had a dedicated IDEA IDE (PyCharm, RubyMine, etc)  But compiling Kotlin? This is JetBrains' premier language. You should be doing everything possible to make compilation and deployment as painless as possible, not leave it for developers to hunt down partial solutions on Stack Overflow and blog sites and Github issues and finally piece something together.

 

0

Gradle as a build system is very flexible and configurable. It is simply not possible to map all possible configurations you can make with Gradle to IntelliJ own artifact builds. So you have a choice to delegate this to the Gradle and the IDE provides this possibility.

0

Please sign in to leave a comment.