How to include an Intellij gradle project inside another Intellij gradle project? - intellij-idea

I have two java projects as Bukkit/Spigot plugins. Both projects are using gradle, private repositories, and one project should inherit from another.
Projects:
SpigotCore - Contains database management and utility classes. This is the "main" project.
Minigame Framework - Runs minigames. Needs database access and utility class access.
What is the best way to make the Minigame Framework inherit from the SpigotCore project using gradle? I have been unsuccessful in getting Intellij module dependency working. Any and all help is appreciated!

I solved this issue by just compiling the SpigotCore and then including the jar file in my build.gradle file.
Answer used: https://stackoverflow.com/a/20956456/2865125
Now following the answer above, it shaded the SpigotCore classes into the MinigameFramework. This is not what we want since both projects are Bukkit plugins and will be provided at runtime. So I changed "compile" to "provided" and it works great!
The change:
dependencies {
provided files("somePath/spigotcore.jar")
}

Related

How does kotlin-gradle-plugin use 'kotlin.code.style' property?

The official Kotlin documentation states:
Add kotlin.code.style=official property to the gradle.properties file at the project root.
I'm trying to understand how kotlin-gradle-plugin handles this property.
Which gradle task uses it?
When running gradle build, I don't see my code being reformatted, even if I format my code badly on purpose.
I went through the Github source code of the plugin but couldn't properly get to understand it.
Thanks for your help.
Kotlin Gradle plugin doesn't use this property, as it's not responsible for reformatting the code. Instead, this property is used by Gradle importer of Kotlin plugin for IntelliJ IDEA.
This facade provides access to Gradle properties defined for the project:
https://github.com/JetBrains/kotlin/blob/v1.4.10/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/GradlePropertiesFileFacade.kt
It checks local.properties first in case user wants to override this value in the local configuration (this file is usually added to .gitignore for VCS to skip it during it's operations), then in usual gradle.properties.
Then the property gets consumed to configure the project here:
https://github.com/JetBrains/kotlin/blob/v1.4.10/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleSourceSetDataService.kt#L158-L159
Same thing goes for Maven-based projects. These are the only two places the property is used throughout Kotlin repository apart from tests right now.

How to add a jar to the external library for a project w/o disappearing after sync and with imports working?

Presently having an annoying time getting a jar added to the external library for the project, then in turn not disappear when syncing and the imports working. Adding I figured out and answered in another question. The issue is to keep persistent and accessible to a jar I create within the project for import.
I am not sure if I am missing a step or forgot to set something. I am using a modified gradle (forgegradle) to create the project. Have not found a solution with the documentation, or any for IntelliJ yet. I am generally new with IntelliJ, choosing to use it instead eclipse which I have previously used.
The goal is a to create an add-on for a mod to another jar. The main jar already in the external library from the start, now attempting to add the mod to it. I could modify the mod, but it is not my code, so rather not simply modify it directly and repackage it.
a jar added to the external library for the project, then in turn not disappear when syncing and the imports working
If you are using an external build tool / system that syncs with IntelliJ it is recommended to use that build tool / system to add dependencies.
IntelliJ will always take a backseat, and treat the build tooling / system as the source of truth for the project model as best it can.
You mention you are using gradle, I would recommend adding the dependency as a managed dependency that is, let gradle download it from a repository, and resolve the dependency/library itself.
If you can not do this, and you can't host a repository yourself, The next best recommendation I have, is creating a 'libs' folder inside the project, that contains jars that can't be found in repositories (They may be mods or plugins that were never published)
You can read up on how to add a library folder to gradle here: How to add local .jar file dependency to build.gradle file?

How to make IntelliJ reference source code in another module?

I have a typical project structure:
- root-dir (not a project)
\- core-module (a gradle project)
\- application (a gradle project)
Both core-module and application are imported into IntelliJ and kept on Auto Import. In application's build.gradle, core-module is referenced as:
compile('my-group-id:core-module:0.2.0-SNAPSHOT')
where 0.2.0-SNAPSHOT is the current version as declared in core-module's gradle.properties.
In application, when I try to view a class from core-module, I'm brought to the source code in core-module-0.2.0-SNAPSHOT-sources.jar, instead of the corresponding source code in the core-module module.
I know I can manually add core-module as a module dependency in application, but next time anything in application's build.gradle changes, auto import will overwrite that dependency.
Is there any way to make IntelliJ recognize automatically that I'm trying to view a class from another module and go there instead of the downloaded sources jar?
Furthermore, is there any way to make IntelliJ always prefer the core-module module over the dependency jar, not only for code viewing, but for building/running/debugging/etc.?
All the source files are in Kotlin, FWIW.
Use Gradle's composite build. Gradle has composite builds https://docs.gradle.org/4.4/userguide/composite_builds.html that allow one to 'include' a build directly, rather than from a repository.
IntelliJ also supports this functionality. This was added in 2016.3.
Here's a webcast:https://blog.jetbrains.com/idea/2017/03/webinar-recording-composite-builds-with-gradle/
In the Gradle tab, right-click on your application module. The menu will have a 'composite builds' option. On the dialog that appears, check the 'core-module' module and close.
Now, right-click on application module and do a 'refresh Gradle project'. I've found if I don't do this, the dependency doesn't get updated correctly.
To verify, look at the dependencies under the sourceSets. Instead of a version #, it will now look like a module dependency.
This provides many benefits. One is the navigation you were looking for. In addition, any changes made in core-module are immediately available, and used for the application.
Refactor a method in core-module that is used by application, and IntelliJ will refactor all usages.
Enjoy!

Gradle project with no project classes but with some buildscript-accessible custom classes?

I am thinking to use Gradle to manipulate with mysql database. It will read some files from filesystem, analyse them and populate database accordingly.
Such project will not produce any project code, because all output will go to database tables. On the other hand, gradle script should access some custom java or groovy classes to facilitate working with source data.
Is this a possible Gradle usage? Where to put gradle-accessible classes then? I don't want to have separate project, producing JAR for this project. I wan't single project, so that Gradle first compiles classes and the utilizes them in the script.
Is this possible?
Gradle is extensible, so you can utilize buildSrc for such scenarios. It works in the following way:
along build.gradle in the project there is buildSrc dir with custom build.gradle
in buildSrc/build.gradle you can define the script dependencies itself, implement plugins and tasks
finally you can apply a plugin from buildSrc to build.gradle.
It's quite handy, since e.g. IntelliJ can import such project and provide code completion for instance.
Another way is to put all the necessary stuff in build.gradle itself.
Such buildSrc project can be compiled to a jar, published and provided as a plugin, or it can be a separate project on github to be downloaded and used to manipulate data. Also, there no need to implement Plugin, you can use static methods e.g. Have a look at the demo.

Maven adds class files of snapshot dependencies

on my Windows machine I do have several proeject that I build with maven. At the moment they are all in SNAPSHOT-State. When I build a project that relies on one of the other projects maven always adds the class files of the other projects to the jar.
If I build the project on my CI-Server this problem does not occur. Does anyone have an idea why maven adds the class files to my jar?
I'm using maven 2.2.1
When I build a project that relies on one of the other projects maven always adds the class files of the other projects to the jar.
This is not a default behavior and, if it happens, you're somehow telling Maven to do so. If you want to hunt potential discrepancies, check the effective-pom, the effective-settings, the active-profiles using the following goals on both machines:
help:effective-pom
help:effective-settings
help:active-profiles
Also double check how Maven is invoked on the CI machine (extra command line parameter, etc).