# 'Parent' build.gradle file

**URL:** <https://discuss.gradle.org/t/parent-build-gradle-file/34868>\
**Category:** Help/Discuss\
**Created:** [February 10, 2020, 9:26pm UTC](https://discuss.gradle.org/t/parent-build-gradle-file/34868 "2020-02-10T21:26:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexgagnon](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@alexgagnon](https://discuss.gradle.org/u/alexgagnon)\
**Post date:** [February 10, 2020, 9:26pm UTC](https://discuss.gradle.org/t/parent-build-gradle-file/34868/1 "2020-02-10T21:26:43Z")

</div>

I originally [posted this on stackoverflow](https://stackoverflow.com/questions/60058302/create-custom-plugin-that-defines-plugins-and-dependencies) but didn’t really get a response, so I figured I’d try it here. I’ve narrowed it down somewhat.

My organization uses the same Gradle plugins and dependencies for a lot of our projects. My custom plugin knowledge is pretty weak, but what I’d like to do is wrap these plugins and dependencies into a single, reusable source file that ‘expands’ in the new project. Here’s a proof of concept:

```groovy
// build.gradle from 'parent' build.gradle
plugins {
  id 'war'
  id 'org.springframework.boot' version '2.2.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'org.asciidoctor.convert' version '1.5.8'
}
	
dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test' {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}
	
test {
 useJUnitPlatform()
}
	

```

and attempting to reference from another project, it’s the only line in the file:

```groovy
	
apply from: '<path-to-above>/build.gradle'
	

```

This error I get is the following:

```gradle
  script '<path-to-above>/build.gradle': 15: Only Project build scripts can contain plugins {} blocks

  See https://docs.gradle.org/5.5.1/userguide/plugins.html#sec:plugins_block for information on the plugins {} block
   @ line 15, column 1.
     plugins {
     ^
  1 error

```

Am I on the right track?

Thanks

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [February 11, 2020, 5:29am UTC](https://discuss.gradle.org/t/parent-build-gradle-file/34868/2 "2020-02-11T05:29:29Z")

</div>

You are not able to use the `plugins { }` block anywhere except the `build.gradle` file for a project. You would need to fall back to `buildscript { }` instead if you use script plugins.

However, script plugins are designed for organizing logic in a project, not for sharing build logic between independent projects. For the case that you describe, a standalone, binary plugin is the preferred approach to share logic between independent projects. Ideally you write tests to verify this logic as well.

Your original post on SO before the edit was much closer to what you should have for the case described, but with a few implementation issues. I’ve provided a more detailed response there on those particular items.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [February 11, 2020, 11:44am UTC](https://discuss.gradle.org/t/parent-build-gradle-file/34868/3 "2020-02-11T11:44:27Z")

</div>

For a plugin to apply other plugins, you may find this approach useful

> [@Apply a gradle plugin (errorprone) from a custom Gradle Java plugin](https://discuss.gradle.org/t/apply-a-gradle-plugin-errorprone-from-a-custom-gradle-java-plugin/34645/2):
>
> I suggest that you apply the plugin by class rather than by ID. This will force you to include the error prone dependency in your plugin’s dependency list as it won’t compile until you add the dependency Eg: import net.ltgt.gradle.errorprone.ErrorPronePlugin.class; public class MyPlugin implements Plugin\<Project\> { public void apply(Project project) { project.getPluginManager().apply(ErrorPronePlugin.class); // custom logic here } }
