[JAVA] Browse local Maven artifacts in Gradle

Browse local Maven artifacts in Gradle

Consider how to set a Maven artifact in Gradle as a dependency, although the source is public, but the artifact is not registered in the public repository.

--Example) Azure Notification Hub: Azure / azure-notificationhubs-java-backend

--How to use --Plain jar with no external dependencies-> Method 1 --Maven jar without external dependency-> Method 2/3 --Maven jar with external dependency-> Method 3

Method 1 --files

--Project structure

- gradle-project
  - build.gradle
  - lib
    - NotificationHubs-0.0.4.jar

build.gradle


dependencies {
    compile files ('lib/NotificationHubs-0.0.4.jar') //Specify by file path
}

problem

--Limited to jar files that do not refer to external modules. --If the jar file is a Maven artifact, the dependencies used by the module specified in files will not be resolved. Run-time error at runtime. --If you can refer to the source file of the jar, you can make the target Maven artifact a fat jar with the ʻassembyorshade` Maven plugin.

Method 2-repositories flatDir

--Project structure --Same as method 1

build.gradle


repositories {
    flatDir {
        dirs "lib" //You can refer to the jar placed in this folder in the usual dependencies notation.
    }
}

dependencies {
    compile('com.windowsazure:NotificationHubs:0.0.4') // groupId/artifactId/version
}

--The external dependency of NotificationHubs has not been resolved. Even if it can be compiled, the class cannot be found at runtime and an error occurs.

NotificationHubsの依存先が出てこない

problem

--Similar to method 1, if the referenced module has additional external dependencies, the dependencies will not be resolved. --Caution when using in multi-project --If you define a flatDir that specifies its own lib in the referenced subproject, the referenced subproject will reference its own lib and will fail to resolve dependencies. - GRADLE-1940 Unresolved dependencies when project A depends on project B and A does not have a reference to the repository used by B --Place lib in the root project and make sure that the subproject refers to lib in the root project.

build.gradle


subprojects {
    repositories {
        flatDir {
            dirs "${rootProject.projectDir}/lib"
        }
    }
}

Method 3 repositories maven (Maven local folder structure)

--Project structure --Use the folder structure of Maven's local repository

- gradle-project
  - build.gradle
  - lib
    - com
      - windowsazure
        -  NotificationHubs
          -  0.0.4
            - NotificationHubs-0.0.4.jar
            - NotificationHubs-0.0.4.pom

build.gradle


repositories {
    maven {
        url "lib"
    }
}

dependencies {
    compile('com.windowsazure:NotificationHubs:0.0.4') // groupId/artifactId/version
}

--The external dependency of Notification Hubs has been resolved.

NotificationHubsの依存先があり全然に利用できる

problem

--Note that there is the same problem as method 2 in the multi-project configuration.

Recommended Posts

Browse local Maven artifacts in Gradle
Popular trends in gradle and maven
Maven repository local cache
Reload dependencies in Maven
Jigsaw notes in Gradle
Dependency management in Gradle using Maven repository on Amazon S3
Copy dependent jars in Gradle 5
Spring Boot 2 multi-project in Gradle
Control tasks performed in Gradle