7
7
import java .nio .file .Files ;
8
8
import java .nio .file .Path ;
9
9
import java .nio .file .Paths ;
10
+ import java .util .Arrays ;
10
11
import java .util .List ;
11
12
12
13
import static com .ibm .northstar .utils .ProjectDirectoryScanner .classFilesStream ;
@@ -17,6 +18,31 @@ public class BuildProject {
17
18
private static final String LIB_DEPS_DOWNLOAD_DIR = "_library_dependencies" ;
18
19
private static final String MAVEN_CMD = System .getProperty ("os.name" ).toLowerCase ().contains ("windows" ) ? "mvn.cmd" : "mvn" ;
19
20
private static final String GRADLE_CMD = System .getProperty ("os.name" ).toLowerCase ().contains ("windows" ) ? "gradlew.bat" : "gradlew" ;
21
+ public static Path tempInitScript ;
22
+ static {
23
+ try {
24
+ tempInitScript = Files .createTempFile ("gradle-init-" , ".gradle" );
25
+ } catch (IOException e ) {
26
+ throw new RuntimeException (e );
27
+ }
28
+ }
29
+ private static final String GRADLE_DEPENDENCIES_TASK = "allprojects { afterEvaluate { project -> task downloadDependencies(type: Copy) {\n " +
30
+ " def configs = project.configurations.findAll { it.canBeResolved }\n \n " +
31
+ " dependsOn configs\n " +
32
+ " from configs\n " +
33
+ " into project.hasProperty('outputDir') ? project.property('outputDir') : \" ${project.buildDir}/libs\" \n \n " +
34
+ " doFirst {\n " +
35
+ " println \" Downloading dependencies for project ${project.name} to: ${destinationDir}\" \n " +
36
+ " configs.each { config ->\n " +
37
+ " println \" Configuration: ${config.name}\" \n " +
38
+ " config.resolvedConfiguration.resolvedArtifacts.each { artifact ->\n " +
39
+ " println \" \t ${artifact.moduleVersion.id}:${artifact.extension}\" \n " +
40
+ " }\n " +
41
+ " }\n " +
42
+ " }\n " +
43
+ " }\n " +
44
+ " }\n " +
45
+ "}" ;
20
46
21
47
private static boolean buildWithTool (String [] buildCommand ) {
22
48
Log .info ("Building the project using " + buildCommand [0 ] + "." );
@@ -30,6 +56,7 @@ private static boolean buildWithTool(String[] buildCommand) {
30
56
Log .info (line );
31
57
}
32
58
int exitCode = process .waitFor ();
59
+ process .getErrorStream ().transferTo (System .err );
33
60
Log .info (buildCommand [0 ].toUpperCase () + " build exited with code " + exitCode );
34
61
return exitCode == 0 ;
35
62
} catch (IOException | InterruptedException e ) {
@@ -73,24 +100,24 @@ private static boolean mavenBuild(String projectPath) {
73
100
return false ;
74
101
}
75
102
String [] mavenCommand = {
76
- MAVEN_CMD , "clean" , "compile" , "-f" , projectPath + "/pom.xml" , "-B" , "-V" , "-e" , "-Drat.skip" ,
77
- "-Dfindbugs.skip" , "-Dcheckstyle.skip" , "-Dpmd.skip=true" , "-Dspotbugs.skip" , "-Denforcer.skip" ,
78
- "-Dmaven.javadoc.skip" , "-DskipTests" , "-Dmaven.test.skip.exec" , "-Dlicense.skip=true" ,
79
- "-Drat.skip=true" , "-Dspotless.check.skip=true" };
103
+ MAVEN_CMD , "clean" , "compile" , "-f" , projectPath + "/pom.xml" , "-B" , "-V" , "-e" , "-Drat.skip" ,
104
+ "-Dfindbugs.skip" , "-Dcheckstyle.skip" , "-Dpmd.skip=true" , "-Dspotbugs.skip" , "-Denforcer.skip" ,
105
+ "-Dmaven.javadoc.skip" , "-DskipTests" , "-Dmaven.test.skip.exec" , "-Dlicense.skip=true" ,
106
+ "-Drat.skip=true" , "-Dspotless.check.skip=true" };
80
107
81
108
return buildWithTool (mavenCommand );
82
109
}
83
110
84
111
public static boolean gradleBuild (String projectPath ) {
85
112
// Adjust Gradle command as needed
86
113
String gradleWrapper = projectPath + File .separator + GRADLE_CMD ;
87
- String [] gradleCommand = { gradleWrapper , "clean" , "compileJava" , "-p" , projectPath };
114
+ String [] gradleCommand = {gradleWrapper , "clean" , "compileJava" , "-p" , projectPath };
88
115
return buildWithTool (gradleCommand );
89
116
}
90
117
91
118
private static boolean buildProject (String projectPath , String build ) {
92
119
File pomFile = new File (projectPath , "pom.xml" );
93
- if (build ==null ) {
120
+ if (build == null ) {
94
121
return true ;
95
122
} else if (build .equals ("auto" )) {
96
123
if (pomFile .exists ()) {
@@ -100,8 +127,7 @@ private static boolean buildProject(String projectPath, String build) {
100
127
Log .info ("Did not find a pom.xml in the project directory. Using Gradle to build the project." );
101
128
return gradleBuild (projectPath ); // Otherwise, use Gradle
102
129
}
103
- }
104
- else {
130
+ } else {
105
131
// Update command with a project path
106
132
build = build .replace (MAVEN_CMD , MAVEN_CMD + " -f " + projectPath );
107
133
Log .info ("Using custom build command: " + build );
@@ -127,31 +153,35 @@ public static List<Path> buildProjectAndStreamClassFiles(String projectPath, Str
127
153
* @param projectPath Path to the project under analysis
128
154
* @return true if dependency download succeeds; false otherwise
129
155
*/
130
- public static boolean downloadLibraryDependencies (String projectPath ) {
156
+ public static boolean downloadLibraryDependencies (String projectPath ) throws IOException {
131
157
// created download dir if it does not exist
132
158
libDownloadPath = Paths .get (projectPath , LIB_DEPS_DOWNLOAD_DIR ).toAbsolutePath ();
133
159
if (!Files .exists (libDownloadPath )) {
134
160
try {
135
161
Files .createDirectory (libDownloadPath );
136
162
} catch (IOException e ) {
137
- Log .error ("Error creating library dependency directory for " + projectPath + ": " +e .getMessage ());
163
+ Log .error ("Error creating library dependency directory for " + projectPath + ": " + e .getMessage ());
138
164
return false ;
139
165
}
140
166
}
141
167
File pomFile = new File (projectPath , "pom.xml" );
142
168
if (pomFile .exists ()) {
143
169
Log .info ("Found pom.xml in the project directory. Using Maven to download dependencies." );
144
170
String [] mavenCommand = {
145
- MAVEN_CMD , "--no-transfer-progress" , "-f" ,
146
- Paths .get (projectPath , "pom.xml" ).toString (),
147
- "dependency:copy-dependencies" ,
148
- "-DoutputDirectory=" + libDownloadPath .toString ()
171
+ MAVEN_CMD , "--no-transfer-progress" , "-f" ,
172
+ Paths .get (projectPath , "pom.xml" ).toString (),
173
+ "dependency:copy-dependencies" ,
174
+ "-DoutputDirectory=" + libDownloadPath .toString ()
149
175
};
150
176
return buildWithTool (mavenCommand );
151
- } else {
152
- // TODO: implement for gradle
153
- return false ;
177
+ } else if (new File (projectPath , "build.gradle" ).exists () || new File (projectPath , "build.gradle.kts" ).exists ()) {
178
+ Log .info ("Found build.gradle[.kts] in the project directory. Using Gradle to download dependencies." );
179
+ tempInitScript = Files .writeString (tempInitScript , GRADLE_DEPENDENCIES_TASK );
180
+ String [] gradleCommand = {projectPath + File .separator + GRADLE_CMD , "--init-script" , tempInitScript .toFile ().getAbsolutePath (), "downloadDependencies" , "-PoutputDir=" +libDownloadPath .toString ()};
181
+ System .out .println (Arrays .toString (gradleCommand ));
182
+ return buildWithTool (gradleCommand );
154
183
}
184
+ return false ;
155
185
}
156
186
157
187
public static void cleanLibraryDependencies () {
@@ -167,5 +197,12 @@ public static void cleanLibraryDependencies() {
167
197
Log .error ("Error deleting library dependency directory: " + e .getMessage ());
168
198
}
169
199
}
200
+ if (tempInitScript != null ) {
201
+ try {
202
+ Files .delete (tempInitScript );
203
+ } catch (IOException e ) {
204
+ Log .error ("Error deleting temporary Gradle init script: " + e .getMessage ());
205
+ }
206
+ }
170
207
}
171
208
}
0 commit comments