[JAVA] Set up Gradle multi-project in IntelliJ to build JAR file

There were many places where I stumbled, so it's an amnesia.

Create a new project

Create a Gradle project. Since it is a root project, uncheck all from "Additional libraries and frameworks". 01.png 02.png 03.png

Add shared project (module)

Right-click "Project View" in IntelliJ and select [New]-> [Module] from the context menu to add a shared module. 04.png

Here, check [Java] in "Additional libraries and frameworks" 05.png 06.png 07.png

It seems like adding from the "Project Structure" window instead of from the "Project View".
(For some reason, the parent project cannot be specified in the "Add as next module" field)
04B.png

When the addition is complete, implement the appropriate common functions. 08.png

Add app module

Next, add the module that uses the common function in the same procedure.

When the addition is complete, add the shared module to dependecies in the" build.gradle "file and implement the appropriate functionality. 09.png

Next, set "Run / Debug Configuration" and check that it can be executed. 10.png 11.png

JAR file generation

In the default state, Gradle's output directory setting and IntelliJ's setting are different, so a normal JAR file is not generated.

Reference: stack overfloe --Building with Intellij 2017.2 / out directory duplicates files in / build directory 12B.png

Therefore, explicitly specify the output directory in the "build.gradle" file of the shared module and app module. 12.png

Next, select [+]-> [JAR]-> [From Dependent Modules ...] from [Artifacts] in "Project Settings" in the "Project Structure" window to add the artifacts. 13.png

Change the manifest file directory from java to resources when specifying the module and main class. 14.png

Finally, make sure that the JAR file can be executed normally. 15.png

Recommended Posts

Set up Gradle multi-project in IntelliJ to build JAR file
Make multi-project executable JAR in Gradle
Set up Multipart Resolver to allow file uploads in Spring
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Add a time stamp to the JAR file name in Gradle
How to add jar file in ScalaIDE
Add jar file obtained from Maven to IntelliJ
How to build an executable jar in Maven
How to output jar with main class specified by gradle in Intellij IDEA
Cache Gradle dependent files to speed up docker build
How to build docker environment with Gradle for intelliJ
Add an external jar file in your IntelliJ project.
How to debug the generated jar file in Eclipse
Build Jar files in plugin folder with one click in IntelliJ
Gradle settings memo (multi-project to all in one) for myself
Create a jar file that can be executed in Gradle
Spring Boot 2 multi-project in Gradle
How to set up a proxy with authentication in Feign
[Gradle] The story that the class file did not exist in the jar file
How to make a jar file with no dependencies in Maven
Unable to get resources when using modules in Gradle and IntelliJ
Fix the file name of war to the one set in Maven
Do HelloWorld in Java / IntelliJ / Gradle
How to run Ant in Gradle
Set up pre-login routing in Devise
Log output to file in Java
Include image resources in jar file
How to set Lombok in Eclipse
Define a task to ZIP archive a set of project files in Gradle
How to read log4j configuration file in Java project summarized in jar file Memo
Set up a Java GUI in a separate thread to keep the main
How to filter JUnit Test in Gradle
Shortcut to automatically generate javadoc in IntelliJ
How to set up and use kapt
Steps to set a favicon in Rails
Configure a multi-project with subdirectories in Gradle
Sample to unzip gz file in Java
Include image resources in jar file
Upload Rails app image file to S3
Image upload
How to set environment variables in the properties file of Spring boot application