由于某种原因,无法找到显示在一个项目中发布jar,然后在另一个项目中使用jar的示例.
因此,我的第一个库项目具有以下build.gradle:
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
version = '1.0.0'
repositories { mavenLocal() }
group = 'com-hlibrary'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
该脚本发布具有以下路径的jar:
~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.jar
同一目录中的pom文件gradle-library-1.0.0.pom如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com-hlibrary</groupId>
<artifactId>gradle-library</artifactId>
<version>1.0.0</version>
</project>
我要使用的Java文件如下:
package com.hlibrary;
import java.time.LocalTime;
public class HourClass {
public static int getHour() {
LocalTime localTime = LocalTime.Now();
return localTime.getHour();
}
}
因此,在我的另一个项目中,我有一个gradle.build,它具有对已构建jar的依赖性:
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
repositories {
jcenter()
mavenLocal()
}
dependencies {
compile ("com.hlibrary:gradle-library:1.0.0")
}
但是我输入后
gradle dependencies
我看到很多这样的错误:
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
\--- com.hlibrary:gradle-library:1.0.0 Failed
解决方法:
我认为问题是您的小组不匹配.
发布时,您正在使用组库.使用依赖项时,您使用的是com.hlibrary组.
这意味着(如您所述)您的.jar将被发布到
~/.m2/repository/com-hlibrary/<project-name>
但是随后您的客户端应用程序正在寻找它
~/.m2/repository/com/hlibrary/<project-name>
如果您对点与破折号一致,那么使用Gradle 4.0可以正常使用此代码.
编辑:
另外,如果您运行gradle clean jar,则会收到比gradle依赖项更有用的错误消息:
* What went wrong:
Could not resolve all files for configuration ':compileClasspath'.
> Could not find com.hlibrary:LibraryTest:1.0.0.
Searched in the following locations:
https://jcenter.bintray.com/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.pom
https://jcenter.bintray.com/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.jar
file:/C:/path/to/home/.m2/repository/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.pom
file:/C:/path/to/home/.m2/repository/com/hlibrary/LibraryTest/1.0.0/LibraryTest-1.0.0.jar
required by:
project :