Gradle Subprojects that do not know that they are subprojects

I am creating a subproject project with several subprojects. The subprojects for each have their own separate git repositories and should be included in the superproject using git submodules. Our preferred case would be that our continuous integration tool (Jenkins) would independently build these subprojects in .jars, not taking into account or not knowing about the superproject.

This can be done quite easily by simply making sure that each of the subprojects has build.gradle, which has all the information needed for the .jar assembly that arises from the assembly. The only complication that arises when some subprojects depend on other subprojects.

Even this is pretty simple if you always want subprojects to pull their ".jars" dependencies from the Maven or Ivy repository. However, when we build our local mailboxes during the development process, we want to be able to create and deploy a super project .war file for Tomcat and all .jars somewhere in the class path and ensure that all .jars are built from a copy of the code that Now located in our own box. (That is, we want it to act basically as if we were using compile (project (": otherproject")) rather than compiling ("group: otherproject: 1.0-SNAPSHOT") or some of them.)

I am new to Gradle, and it is possible that I am missing something quite obvious, but I have not yet found a good way to handle this.

We could do something so that the set of dependencies for building it by pulling .jars from our Maven repository is in one file and the set of dependencies for building it using its sibling subprojects in another file, but this looks like a Dry Break. (In this case, we would have two separate places to manage the dependency list.)

We could create subproject assembly scripts to always fetch Maven from the repository and have a superproject script design to include sections for each subproject that redefine the dependencies in the subprojects, but this is equivalent to a DRY violation as the previous option.

I thought that it is possible that the superproject really inherits and modifies the dependency lists on each of the subprojects as necessary (the code in the superproject must have access to all the information necessary to convert the dependency on the Maven artifact into the corresponding dependency on the sibling subproject, I would have thought), but, having studied it quite a bit, I'm not sure how this could be done.

I hope you people can help. Thanks!

+6
source share
2 answers

Dynamically switching between project and external dependencies is currently not a first-class feature in Gradle. However, there is a Prezi Pride tool, and I highly recommend giving it a spin.

+4
source

You can do what you want by connecting dependency resolution to gradle.

In pseudo code and ignoring error paths:

depHandler = project.getDependencies() oldMM = depHandler.metaClass.getMetaMethod('methodMissing', [String.class, Object.class] as Class[]) depHandler.metaClass.methodMissing = { String n, def args -> depLocator = args[0] name = getDepName(depLocator) depProject = tryFindProject(project, name) if (depProject) args[0] = depProject oldMM.invoke(depHandler, name, args) } project.setDependencyHandler(depHandler) 

Where getDepName will receive the dependency locator (either the string 'group: name: version', or map {group =, name =, version =}), tryFindProject will see if there is a project named 'name' and if so. (Perhaps ': $ name' will be returned)

This, of course, assumes that your project names exactly match the names of the artifacts and ignore the group / version. You can get around the first by renaming projects by including them in settings.gradle: include 'name'; project(":${name}").projectDir = dir include 'name'; project(":${name}").projectDir = dir .

+1
source

Source: https://habr.com/ru/post/970240/


All Articles