【问题标题】:How to use in gradle a jar from the Local Maven repository如何在本地 Maven 存储库中使用 gradle jar
【发布时间】:2018-05-23 16:42:02
【问题描述】:

由于某种原因,找不到显示在一个项目中发布 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

【问题讨论】:

  • implementation ("com-hlibrary:gradle-library:1.0.0") 在你的 pom 中 groupId 是 com-hlibrary ...试试看
  • 那么“gradle依赖”没有报错,但是还是不能使用这个Library类
  • 您发布到mavenCentral(),但随后您在repositories 部分的mavenLocal() 中查找您的库。尝试在那里添加mavenCentral()
  • 试过了,没用

标签: java maven gradle


【解决方案1】:

试试吧

compile ("com.hlibrary:gradle-library:1.0.0 )

格式为“group:artifactId:version”

你可以在

中查看.pom文件

~/.m2/repository/com-hlibrary/gradle-library/1.0.0/gradle-library-1.0.0.pom

【讨论】:

  • 谢谢,我按照你的建议改了build.gradle,还是一样,看pom文件
【解决方案2】:

我认为问题在于您的groups 不匹配。

当您发布时,您使用的是群组com-hlibrary。当您使用依赖项时,您正在使用组com.hlibrary


这意味着(如您所述)您的 .jar 正在发布到

~/.m2/repository/com-hlibrary/<project-name>

但是您的客户端应用程序正在寻找它

~/.m2/repository/com/hlibrary/<project-name>

如果您与点与破折号一致,则此代码使用 Gradle 4.0 对我来说可以正常工作。

编辑

此外,如果您运行 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 :

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2016-12-02
    • 1970-01-01
    • 2014-10-03
    相关资源
    最近更新 更多