[英]How to use in gradle a jar from the Local Maven repository
For some reason cannot find an example that shows publishing a jar in one project and then using this jar in another project. 由于某种原因,找不到一个示例,显示在一个项目中发布jar,然后在另一个项目中使用此jar。
So, my first library project has the following build.gradle: 所以,我的第一个库项目有以下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
}
}
}
This script publishes a jar with the following path: 此脚本使用以下路径发布jar:
~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.jar
The pom file gradle-library-1.0.0.pom in the same directory is as follows: 同一目录下的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>
The Java file I want to use is as follows: 我想要使用的Java文件如下:
package com.hlibrary;
import java.time.LocalTime;
public class HourClass {
public static int getHour() {
LocalTime localTime = LocalTime.now();
return localTime.getHour();
}
}
So, in my another project I have a gradle.build with the dependency to the built jar: 所以,在我的另一个项目中,我有一个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")
}
However after I type 但是在我打字之后
gradle dependencies
I see numerous errors like this: 我看到很多这样的错误:
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
\--- com.hlibrary:gradle-library:1.0.0 FAILED
I think the problem is that your group
s don't match. 我认为问题是你的
group
不匹配。
When you publish , you're using group com-hlibrary
. 发布时 ,您正在使用群组
com-hlibrary
。 When you consume the dependency, you're using group com.hlibrary
. 当您使用依赖项时,您正在使用组
com.hlibrary
。
This means that (as you noted) your .jar is being published to 这意味着(正如您所说)您的.jar正在发布
~/.m2/repository/com-hlibrary/<project-name>
But then your client application is looking for it in 但是,您的客户端应用程序正在寻找它
~/.m2/repository/com/hlibrary/<project-name>
If you're consistent with dot vs. dash, this code works as expected for me using Gradle 4.0. 如果你与点对点破折号一致,那么这段代码就像我预期的那样使用Gradle 4.0。
Edit : 编辑 :
Also, if you run gradle clean jar
, you'll get a much more useful error message than gradle dependencies
gives you: 此外,如果您运行
gradle clean jar
,你会得到比一个更有用的错误消息gradle dependencies
为您提供:
* 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 :
Try instead 试试吧
compile ("com.hlibrary:gradle-library:1.0.0 )
The format is "group:artifactId:version" 格式为“group:artifactId:version”
you can check the .pom file in 你可以检查.pom文件
~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.pom 〜/ .m2目录/库/ COM-hlibrary / gradle这个库/ 1.0.0 / gradle这个库,1.0.0.pom
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.