How to call project(path: ':someProject', configuration: 'archives') outside of dependencies { }

Background:
First off I have a somewhat interesting Gradle build setup with multi-projects. I’ve created a set of tools that allows me to get dependencies from a subproject (if it exists locally) or if its missing it uses a maven dependency…

Essentially what I do in my settings.gradle is I search the parent directory for several project names, if the directory is found I do an “include someProject”

Then in my build.gradle file I have the following method

def flexibleDependency(projectName, notation) {
	def depProject = project(":$projectName")
	if (depProject.getProjectDir().exists()) {
		depProject
	} else {
		dependencies.create(notation)
	}
}

and have the following dependencies closure…

dependencies {
	compile flexibleDependency('someproject', 'com.foo.test:someproject:1.0.0.+')
	compile flexibleDependency('someproject2', 'com.foo.bar:someproject2:1.0.0.+')	
}

This allows developers to have option to check out the ‘someproject’ locally and build as a multi-project or don’t check out the someproject and just pull the latest built artifact from our published artifacts…

This has been working very well and saves tons of times if you need to make significant changes to dependent projects without kicking off a million CI’s and waiting.

The Problem:
There is a project that needs a subprojects “archives” configuration for a war file. Gradle seems to only set the default configuration for dependent projects.

You get around this by doing:

dependencies {
	myconfig project(path: ':someproject', configuration: 'archives')	
}

My problem is I cannot access project(path: ‘someproject’, configuration: ‘archives’) outside of the dependencies closure.

What I want is this:

def flexibleDependency(projectName, notation) {
	def depProject = project(path: ":$projectName", configuration: 'archives')
	if (depProject.getProjectDir().exists()) {
		depProject
	} else {
		dependencies.create(notation)
	}
}

But I get the error:

Could not find method project() for arguments [{path=:someproject, configuration=archives}] on root project ‘myrootproject’.

I’m really stuck here because my dependencies inclusion is conditional and without a flexibleDependencies method developers will have comment out one or the other just to get gradle to not barf…

Any suggestions, hacks, or magic?

DEAR PEOPLE OF THE FUTURE:

I have solved this problem by tackling it from another direction. Since I could not select the ‘archives’ configuration, I decided to just alter the ‘default’ configuration in the actual dependent project.

This might not solve the problem for all cases, but it was a good enough workaround for me to get moving again…

Jar’s get added to the default configuration by default, but non-jar artifacts do not. To fix this just add the following to your dependencies build.grade

artifacts.add('default', war)

Where war could be any other artifact task or archive…

Just so you know, you can access the configuration of another project by doing:

project(’:path:to:project’).configurationName

You have to ensure that the project you’re referring to is evaluated before this one by doing evaluationDependsOn

Hope that helps :slight_smile: