Skip to content

Commit cd4b526

Browse files
denis-zhdanovmaibin
authored andcommitted
BAEL-1451 Writing a Jenkins plugin (eugenp#3396)
* BAEL-1451 Writing a Jenkins plugin A sample Jenkins plugin which builds basic project stats * BAEL-1451 Writing a Jenkins plugin Formatting
1 parent 6f3710b commit cd4b526

4 files changed

Lines changed: 252 additions & 0 deletions

File tree

‎jenkins/hello-world/pom.xml‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.jenkins-ci.plugins</groupId>
6+
<artifactId>plugin</artifactId>
7+
<version>2.33</version>
8+
<relativePath />
9+
</parent>
10+
<artifactId>jenkins-hello-world</artifactId>
11+
<version>1.0-SNAPSHOT</version>
12+
<packaging>hpi</packaging>
13+
<properties>
14+
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
15+
<jenkins.version>2.7.3</jenkins.version>
16+
</properties>
17+
<name>Hello World Plugin</name>
18+
<description>A sample Jenkins Hello World plugin</description>
19+
<url>https://wiki.jenkins-ci.org/display/JENKINS/TODO+Plugin</url>
20+
<licenses>
21+
<license>
22+
<name>MIT License</name>
23+
<url>http://opensource.org/licenses/MIT</url>
24+
</license>
25+
</licenses>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.jenkins-ci.plugins</groupId>
29+
<artifactId>structs</artifactId>
30+
<version>1.7</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
34+
<artifactId>workflow-step-api</artifactId>
35+
<version>2.12</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
40+
<artifactId>workflow-cps</artifactId>
41+
<version>2.39</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
46+
<artifactId>workflow-job</artifactId>
47+
<version>2.11.2</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
52+
<artifactId>workflow-basic-steps</artifactId>
53+
<version>2.6</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
58+
<artifactId>workflow-durable-task-step</artifactId>
59+
<version>2.13</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
64+
<artifactId>workflow-api</artifactId>
65+
<version>2.20</version>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
70+
<artifactId>workflow-support</artifactId>
71+
<version>2.14</version>
72+
<scope>test</scope>
73+
</dependency>
74+
</dependencies>
75+
76+
<repositories>
77+
<repository>
78+
<id>repo.jenkins-ci.org</id>
79+
<url>https://repo.jenkins-ci.org/public/</url>
80+
</repository>
81+
</repositories>
82+
83+
<pluginRepositories>
84+
<pluginRepository>
85+
<id>repo.jenkins-ci.org</id>
86+
<url>https://repo.jenkins-ci.org/public/</url>
87+
</pluginRepository>
88+
</pluginRepositories>
89+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.jenkins.helloworld;
2+
3+
public class ProjectStats {
4+
5+
private final int classesNumber;
6+
private final int linesNumber;
7+
8+
public ProjectStats(int classesNumber, int linesNumber) {
9+
this.classesNumber = classesNumber;
10+
this.linesNumber = linesNumber;
11+
}
12+
13+
public int getClassesNumber() {
14+
return classesNumber;
15+
}
16+
17+
public int getLinesNumber() {
18+
return linesNumber;
19+
}
20+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.baeldung.jenkins.helloworld;
2+
3+
import hudson.Extension;
4+
import hudson.FilePath;
5+
import hudson.Launcher;
6+
import hudson.model.AbstractBuild;
7+
import hudson.model.AbstractProject;
8+
import hudson.model.BuildListener;
9+
import hudson.tasks.BuildWrapper;
10+
import hudson.tasks.BuildWrapperDescriptor;
11+
import org.kohsuke.stapler.DataBoundConstructor;
12+
13+
import javax.annotation.Nonnull;
14+
import java.io.*;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.Stack;
17+
18+
public class ProjectStatsBuildWrapper extends BuildWrapper {
19+
20+
private static final String REPORT_TEMPLATE_PATH = "/stats.html";
21+
private static final String PROJECT_NAME_VAR = "$PROJECT_NAME$";
22+
private static final String CLASSES_NUMBER_VAR = "$CLASSES_NUMBER$";
23+
private static final String LINES_NUMBER_VAR = "$LINES_NUMBER$";
24+
25+
@DataBoundConstructor
26+
public ProjectStatsBuildWrapper() {
27+
}
28+
29+
@Override
30+
public Environment setUp(AbstractBuild build, final Launcher launcher, BuildListener listener) {
31+
return new Environment() {
32+
@Override
33+
public boolean tearDown(AbstractBuild build, BuildListener listener)
34+
throws IOException, InterruptedException
35+
{
36+
ProjectStats stats = buildStats(build.getWorkspace());
37+
String report = generateReport(build.getProject().getDisplayName(), stats);
38+
File artifactsDir = build.getArtifactsDir();
39+
if (!artifactsDir.isDirectory()) {
40+
boolean success = artifactsDir.mkdirs();
41+
if (!success) {
42+
listener.getLogger().println("Can't create artifacts directory at "
43+
+ artifactsDir.getAbsolutePath());
44+
}
45+
}
46+
String path = artifactsDir.getCanonicalPath() + REPORT_TEMPLATE_PATH;
47+
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),
48+
StandardCharsets.UTF_8))) {
49+
writer.write(report);
50+
}
51+
return super.tearDown(build, listener);
52+
}
53+
};
54+
}
55+
56+
private static ProjectStats buildStats(FilePath root) throws IOException, InterruptedException {
57+
int classesNumber = 0;
58+
int linesNumber = 0;
59+
Stack<FilePath> toProcess = new Stack<>();
60+
toProcess.push(root);
61+
while (!toProcess.isEmpty()) {
62+
FilePath path = toProcess.pop();
63+
if (path.isDirectory()) {
64+
toProcess.addAll(path.list());
65+
} else if (path.getName().endsWith(".java")) {
66+
classesNumber++;
67+
linesNumber += countLines(path);
68+
}
69+
}
70+
return new ProjectStats(classesNumber, linesNumber);
71+
}
72+
73+
private static int countLines(FilePath path) throws IOException, InterruptedException {
74+
byte[] buffer = new byte[1024];
75+
int result = 1;
76+
try (InputStream in = path.read()) {
77+
while (true) {
78+
int read = in.read(buffer);
79+
if (read < 0) {
80+
return result;
81+
}
82+
for (int i = 0; i < read; i++) {
83+
if (buffer[i] == '\n') {
84+
result++;
85+
}
86+
}
87+
}
88+
}
89+
}
90+
91+
private static String generateReport(String projectName, ProjectStats stats) throws IOException {
92+
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
93+
try (InputStream in = ProjectStatsBuildWrapper.class.getResourceAsStream(REPORT_TEMPLATE_PATH)) {
94+
byte[] buffer = new byte[1024];
95+
int read;
96+
while ((read = in.read(buffer)) >= 0) {
97+
bOut.write(buffer, 0, read);
98+
}
99+
}
100+
String content = new String(bOut.toByteArray(), StandardCharsets.UTF_8);
101+
content = content.replace(PROJECT_NAME_VAR, projectName);
102+
content = content.replace(CLASSES_NUMBER_VAR, String.valueOf(stats.getClassesNumber()));
103+
content = content.replace(LINES_NUMBER_VAR, String.valueOf(stats.getLinesNumber()));
104+
return content;
105+
}
106+
107+
@Extension
108+
public static final class DescriptorImpl extends BuildWrapperDescriptor {
109+
110+
@Override
111+
public boolean isApplicable(AbstractProject<?, ?> item) {
112+
return true;
113+
}
114+
115+
@Nonnull
116+
@Override
117+
public String getDisplayName() {
118+
return "Construct project stats during build";
119+
}
120+
121+
}
122+
123+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>$PROJECT_NAME$</title>
6+
</head>
7+
<body>
8+
Project $PROJECT_NAME$:
9+
<table border="1">
10+
<tr>
11+
<th>Classes number</th>
12+
<th>Lines number</th>
13+
</tr>
14+
<tr>
15+
<td>$CLASSES_NUMBER$</td>
16+
<td>$LINES_NUMBER$</td>
17+
</tr>
18+
</table>
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)