Don’t follow this if you can make a gradle submodule. That is much simpler
This is for adding a local project as an instant dependency.
a) How to add micronaut library inspired by Modularizing with Micronaut Framework | kreuzwerker but changing to import a local project instead of using lib/xxx.jar
b) How to add a local standalone gradle project inspired by java - Gradle Local Project Dependency - Stack Overflow but change compile project("…") to implement project("…")
// parent settings.gradle
include ':GradleProjectLib'
project(':GradleProjectLib').projectDir = new File('../library')
// parent build.gradle
dependencies {
...
implement project(":GradleProjectLib")
...
}
// library build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
//...
}
java {
sourceCompatibility = JavaVersion.toVersion("17")
targetCompatibility = JavaVersion.toVersion("17")
}
c) micronaut library instead of plain java library inspired by Micronaut Gradle plugin
// library gradle.properties
micronautVersion=3.6.1
// library build.gradle
plugins {
// id("io.micronaut.library") version "3.5.1" // cannot have version specified
id("io.micronaut.library")
}
dependencies {
...
}
// parent src/main/java/.../Application.java
public class Application {
public static void main(String[] args) {
// Micronaut.run(Application.class, args);
Micronaut.build(args)
.classes(Application.class, AnyClassInThePackageOfYourLibrary.class)
.start();
}
}
d) needed to split the tests and code for the library
- Lib plugin cannot have version specified to be included in parent -> lib
- Lib plugin must have version specified to run the tests -> lib-test
// library-test gradle.properties
micronautVersion=3.6.1
// library-test build.gradle
plugins {
id("io.micronaut.application") version "3.5.1" // must have version specified
}
dependencies {
//...
implementation project(":GradleProjectLib")
//...
}
application {
mainClass.set("<packages>.Application")
}
// library-test settings.gradle
include ':GradleProjectLib'
project(':GradleProjectLib').projectDir = new File('../library')
// library-test src/main/java/<packages>/Application.java
// needed to add a dummy micronaut entrypoint for the tests to run
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
// library-test src/main/resources/application.yml
// ...