简体   繁体   English

什么是适当的Gradle项目结构,用于在“项目”和“外部”依赖项之间切换

[英]What is the proper Gradle project structure for switching between 'project' and 'external' dependencies

I have this small set of libraries and app: 我有一小组库和应用程序:

  • forge-base - java project (Intellij IDEA project) forge forge-base - java项目(Intellij IDEA项目)
  • forge-android - android library project, depends on forge-base (AS project) forge-android - android库项目,取决于forge-base (AS项目)
  • forge-android-skeleton - sample android app, depends on forge-android (AS project) forge-android-skeleton - 示例android应用程序,取决于forge-android (AS项目)

During the initial development I used structure with project dependencies like: 在初始开发期间,我使用了具有项目依赖性的结构,例如:

settings.gradle: settings.gradle:

...

include ':forge-base'
project(':forge-base').projectDir=new File("$settingsDir/../forge/base")

...

and then in build.gradle: 然后在build.gradle中:

compile project(':forge-base')

This worked like a charm but later I needed to publish the libs on maven repo and dependencies had to be changed like: 这有点像魅力,但后来我需要在maven repo上发布libs,依赖关系必须改变如下:

build.gradle: 的build.gradle:

compile 'com.bolyartech.forge:forge-base:2.0-SNAPSHOT'

The problem that I am facing now is that I am trying to do some major refactoring in all the 3 projects and I need the old deps structure in order easily to confirm the consistency of the projects, eg to build just the skeleton app and all the recursive recompile/building to take place automatically (as it does when a lib project is referenced with compile project(':forge-base') ). 我现在面临的问题是我试图在所有3个项目中进行一些重大的重构,我需要旧的deps结构,以便轻松确认项目的一致性,例如构建skeleton应用程序和所有递归重新编译/构建自动发生(就像使用compile project(':forge-base')引用lib项目时那样compile project(':forge-base') )。 If I use the 'new' structure with publishing to the (local) maven I have to publish the lib each time (with incremented version) I change something in it in order changes to be visible by the other two projects. 如果我使用'new'结构发布到(本地)maven我必须每次都发布lib(增加版本)我在其中更改一些内容,以便其他两个项目可以看到更改。

What (is there) is the usual/canonical why to handle situations like this? 什么(有)通常/规范为什么要处理这样的情况?

Is there an easy way to switch between the two 'modes', eg 'internal' / 'external' dependencies? 有没有一种简单的方法可以在两种“模式”之间切换,例如“内部”/“外部”依赖?

It turns out it is pretty easy to do it. 事实证明这很容易做到。 You can use different dependencies for the different build types, ie debug/release so for example for my forge-android-skeleton project now I have the following: 您可以为不同的构建类型使用不同的依赖项,即调试/发布,例如对于我的forge-android-skeleton项目,现在我有以下内容:

in settings.gradle : settings.gradle

include ':app'

include ':forge-base' project(':forge-base').projectDir=new
File("$settingsDir/../../../forge/base")

include ':forge-android' project(':forge-android').projectDir=new
File("$settingsDir/../../../forge-android/forge-android")

in app/build.gradle : app/build.gradle

...
releaseCompile ('com.bolyartech.forge:forge-android:2.7-SNAPSHOT')

debugCompile project(':forge-android')
...

Please note that in the settings.gradle you need to have all the dependencies back to the bottom otherwise it will not work (that is the reason forge-base is defined there even not excplicitly used). 请注意,在settings.gradle您需要将所有依赖项都放回到底部,否则它将无法工作(这就是在那里定义了forge forge-base甚至没有明确使用的原因)。

You can also define yet another build type like direct-deps and use it in case you don't like to mess with debug/release types. 您还可以定义另一种构建类型,如direct-deps ,并在您不喜欢混淆调试/发布类型时使用它。

Please note that in case you are using different IDEs for some of the projects (like in my case IDEA and AS) probably it will be good idea to ensure that both are using same (version of) gradle otherwise unexpected problems may occur. 请注意,如果您为某些项目(例如我的IDEA和AS)使用不同的IDE,可能最好确保两者都使用相同(版本)gradle,否则可能会出现意外问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM