[英]Replace external jar dependency with local intellij project
So I have an intellij IDEA project (project A) which includes a bunch of external jar libraries.所以我有一个 intellij IDEA 项目(项目 A),其中包括一堆外部 jar 库。 I am currently working on one of those external jar libraries (project B) and I want to replace it with the local project (project B) on my computer.
我目前正在处理这些外部 jar 库之一(项目 B),我想用我计算机上的本地项目(项目 B)替换它。
So, in short: I have Project A which depends on jar BI want to replace jar B with my local project (project B)所以,简而言之:我的项目 A 依赖于 jar BI 想用我的本地项目(项目 B)替换 jar B
That way, when I run project A, it uses Project B local rather than Jar B external.这样,当我运行项目 A 时,它使用本地项目 B 而不是外部 Jar B。 Anyone know any good easy ways of doing this?
任何人都知道这样做有什么好的简单方法吗?
Your use case is exactly what composite builds in Gradle are made for.您的用例正是 Gradle 中的复合构建的用途。 The docs even mention your precise use case:
文档甚至提到了您的确切用例:
Composite builds allow you to […] combine builds that are usually developed independently, for instance when trying out a bug fix in a library that your application uses
复合构建允许您 [...] 组合通常独立开发的构建,例如在您的应用程序使用的库中尝试修复错误时
A composite build allows you to temporarily and easily replace an external dependency library with a locally available build project of that library.复合构建允许您临时轻松地将外部依赖库替换为该库的本地可用构建项目。 (It would even work with multiple different dependencies.)
(它甚至可以与多个不同的依赖项一起使用。)
Here's how you'd set this up for your two projects (leaving out Gradle Wrapper files and Java source code for conciseness):以下是您如何为您的两个项目设置它(为简洁起见,省略了Gradle 包装文件和 Java 源代码):
├── projectA
│ ├── build.gradle
│ └── settings.gradle
└── projectB
├── build.gradle
└── settings.gradle
Note that the two project directories don't actually have to be siblings.请注意,这两个项目目录实际上不必是同级目录。 I've only done that here to keep it simple.
我只是在这里这样做以保持简单。 In my minimal sample build, the two
settings.gradle
files can be empty.在我的最小示例构建中,两个
settings.gradle
文件可以为空。 The two build scripts look as follows.这两个构建脚本如下所示。
projectA/build.gradle
plugins {
id 'java'
}
dependencies {
implementation 'com.example:projectB:1.2.+'
}
repositories {
// where you get the projectB dependency from normally
}
projectB/build.gradle
plugins {
id 'java-library'
}
group = 'com.example'
version = '1.2.3'
Under projectA/
, simply run ./gradlew --include-build../projectB build
(or any other Gradle task(s) you're interested in).在
projectA/
下,只需运行./gradlew --include-build../projectB build
(或您感兴趣的任何其他 Gradle 任务)。 Make sure to use the right path to the directory of projectB.确保使用正确的 projectB 目录路径。 The
--include-build
option automatically creates the composite build on-the-fly. --include-build
选项会自动即时创建复合构建。
You can also create a composite build in IntelliJ.您还可以在 IntelliJ 中创建复合构建。 To do that:
要做到这一点:
build.gradle
file to make sure that IntelliJ automatically uses the Gradle configuration).build.gradle
文件以确保 IntelliJ 自动使用 Gradle 配置)。build.gradle
file of projectB.build.gradle
文件。 That's it.就是这样。 You should now be able to use projectA with its locally available dependency projectB.
您现在应该能够使用 projectA 及其本地可用的依赖项 projectB。
You need to use Maven to manage project dependencies.您需要使用 Maven 来管理项目依赖关系。
Since you have the tag "gradle", I'm assuming you are using it for both projects.因为你有标签“gradle”,我假设你在两个项目中都使用它。 Run "gradle publishToMavenLocal" in project B and then refresh the dependencies of project A.
在项目B中运行“gradle publishToMavenLocal”,然后刷新项目A的依赖。
Just make sure you remember you did this and delete project B from your local repository when you are done with it.只要确保你记得你做过这件事,并在完成后从你的本地存储库中删除项目 B。
Solution with gradle: gradle 的解决方案:
In Project A
build.gradle add mavenLocal()
to the repositories.在项目
A
build.gradle 中,将mavenLocal()
添加到存储库。
...
repositories {
mavenLocal()
maven { url ... }
}
...
In Project B
you need the maven-publish
plugin, and the related task.在项目
B
中,您需要maven-publish
插件和相关任务。
plugins {
id 'maven-publish'
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
classifier = 'sources'
}
publishing {
publications {
mavenJava(MavenPublication) {
artifactId = rootProject.name //or some string eg.: 'projectb'
groupId = project.group //or some string eg.: 'com.example'
version = project.version //or some string eg.: '1.0.3.9'
from components.java
artifact sourcesJar
}
}
}
Now you can run the publishToMavenLocal
task from your terminal, or from Gradle View
in IntelliJ.现在您可以从您的终端运行
publishToMavenLocal
任务,或者从Gradle View
。 (It is on the right side by default, gradle tab, expand your project and bellow publishing you can find the task.) (默认在右侧,gradle tab,展开你的项目,在发布下方可以找到任务。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.