java 如何将 Gradle jar 编译的安装到本地 Gradle Repository files-2.1 而不是 Maven 存储库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47685487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:44:44  来源:igfitidea点击:

How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository?

javagradlejar

提问by Alex

In my build.gradle, I added the plugin:

在我的 build.gradle 中,我添加了插件:

apply plugin: 'maven'

Then using “gradle install” I can copy the resulted jar into the maven repository : ~/.m2/repository

然后使用“gradle install”我可以将生成的 jar 复制到 maven 存储库中:~/.m2/repository

However, my Gradle repository resides in ~/.gradle/caches/modules-2/files-2.1. How can I install the jar into this repository?

但是,我的 Gradle 存储库位于 ~/.gradle/caches/modules-2/files-2.1。如何将 jar 安装到此存储库中?

采纳答案by ddarellis

What worked for me is gradle install -Dmaven.repo.local=the/path/of/the/folder.

对我有用的是gradle install -Dmaven.repo.local=the/path/of/the/folder.

I don't know which IDE you are using but in eclipse you can add a new Run Configuration, in Gradle taks add installand in program arguments -Dmaven.repo.local=the/path/of/the/folder.

我不知道您使用的是哪个 IDE,但在 eclipse 中您可以添加一个新的运行配置,在 Gradle taks addinstall和 program arguments 中-Dmaven.repo.local=the/path/of/the/folder

回答by Danio

If you insist on manipulating the cache, then your best bet is to write a shell script that will manually replace latest JAR in the cache.

如果您坚持操作缓存,那么最好的办法是编写一个 shell 脚本,该脚本将手动替换缓存中最新的 JAR。

The reason is that Gradle does not come with this functionality built-in, as Gradle uses notion of "local cache" in a strict sense, as opposed to "local repository" which is used by Maven.

原因是 Gradle 没有内置此功能,因为 Gradle 在严格意义上使用“本地缓存”的概念,而不是 Maven 使用的“本地存储库”。

The difference is that you are never supposed to save files to local cache manually.

不同之处在于您永远不应该手动将文件保存到本地缓存。

To solve your problem the recommended way: Suppose that project A is a dependency of project B. Then you can call publishToMavenLocalcommand in project A to refresh the depedency. Add mavenLocal()repository in gradle.buildof project B, so every time you build project B, Gradle will check the local repository when resolving the dependency A.

要解决您的问题,推荐方法:假设项目A是项目B的依赖项。那么您可以publishToMavenLocal在项目A中调用命令来刷新依赖项。mavenLocal()gradle.build项目B中添加repository ,所以每次构建项目B时,Gradle在解析依赖A时都会检查本地仓库。

回答by Wembo Mulumba

mavenLocal() is resolved like this:

mavenLocal() 是这样解决的:

In order to make use of a non-standard local maven repository, you can use the following configuration in your build.gradle:

为了使用非标准的本地 maven 存储库,您可以在 build.gradle 中使用以下配置:

repositories { maven { url '/Users/manuelj/apache/maven/repository' } }

存储库 { maven { url '/Users/manuelj/apache/maven/repository' } }

回答by Wembo Mulumba

A build.gradle sample to create a Jar file along with its logback dependencies. using mavenlocale()

用于创建 Jar 文件及其 logback 依赖项的 build.gradle 示例。使用 mavenlocale()

apply plugin: 'java'

apply plugin: 'eclipse'

version = '1.0'

sourceCompatibility = 1.7

target Compatibility = 1.7

//create a single Jar with all dependencies

 task fatJar(type: Jar) {

     manifest {

        attributes 'Implementation-Title': 'Gradle Jar File Example',

            'Implementation-Version': version,

            'Main-Class': 'com.mkyong.DateUtils'
    }
    baseName = project.name + '-all'

    from { configurations.compile.collect { it.isDirectory() ? it : 
zipTree(it)

 } 

}
    with jar
}

//Get dependencies from Maven central repository

repositories {

    mavenCentral()
}

//Project dependencies

dependencies {

    compile 'ch.qos.logback:logback-classic:1.1.2'

}

Reference create a Jar file along with its logback dependencies.

参考创建一个 Jar 文件及其 logback 依赖项。