-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ProjectBuilder.java
123 lines (112 loc) · 3.46 KB
/
ProjectBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.testfixtures;
import org.gradle.api.Project;
import org.gradle.testfixtures.internal.ProjectBuilderImpl;
import javax.annotation.Nullable;
import java.io.File;
/**
* <p>Creates dummy instances of {@link org.gradle.api.Project} which you can use in testing custom task and plugin
* implementations.</p>
*
* <p>To create a project instance:</p>
*
* <ol>
*
* <li>Create a {@code ProjectBuilder} instance by calling {@link #builder()}.</li>
*
* <li>Optionally, configure the builder.</li>
*
* <li>Call {@link #build()} to create the {@code Project} instance.</li>
*
* </ol>
*
* <p>You can reuse a builder to create multiple {@code Project} instances.</p>
*
* <p>The {@code ProjectBuilder} implementation bundled with Gradle 3.0 and 3.1 suffers from a
* binary compatibility issue exposed by applying plugins compiled with Gradle 2.7 and earlier.
* Applying those pre-compiled plugins in a ProjectBuilder context will result in a {@link ClassNotFoundException}.</p>
*/
public class ProjectBuilder {
private File projectDir;
private File gradleUserHomeDir;
private String name = "test";
private Project parent;
private final ProjectBuilderImpl impl = new ProjectBuilderImpl();
/**
* An instance should only be created via the {@link #builder()}.
*/
private ProjectBuilder() { }
/**
* Creates a project builder.
*
* @return The builder
*/
public static ProjectBuilder builder() {
return new ProjectBuilder();
}
/**
* Specifies the project directory for the project to build.
*
* @param dir The project directory
* @return The builder
*/
public ProjectBuilder withProjectDir(@Nullable File dir) {
projectDir = dir;
return this;
}
/**
* Specifies the Gradle user home for the builder. If not set, an empty directory under the project directory
* will be used.
*
* @return The builder
*/
public ProjectBuilder withGradleUserHomeDir(@Nullable File dir) {
gradleUserHomeDir = dir;
return this;
}
/**
* Specifies the name for the project
*
* @param name project name
* @return The builder
*/
public ProjectBuilder withName(String name) {
this.name = name;
return this;
}
/**
* Specifies the parent project. Use it to create multi-module projects.
*
* @param parent parent project
* @return The builder
*/
public ProjectBuilder withParent(@Nullable Project parent) {
this.parent = parent;
return this;
}
/**
* Creates the project.
*
* @return The project
*/
public Project build() {
if (parent != null) {
return impl.createChildProject(name, parent, projectDir);
}
return impl.createProject(name, projectDir, gradleUserHomeDir);
}
}