Skip to content

Commit 0118fce

Browse files
timjajtnord
andauthored
Fork jenkins with winstone instead of jetty plugin (#862)
Co-authored-by: James Nord <jtnord@users.noreply.github.com>
1 parent 92bd7fc commit 0118fce

File tree

6 files changed

+477
-661
lines changed

6 files changed

+477
-661
lines changed

pom.xml

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,6 @@
5555

5656
<dependencyManagement>
5757
<dependencies>
58-
<dependency>
59-
<groupId>org.eclipse.jetty</groupId>
60-
<artifactId>jetty-bom</artifactId>
61-
<version>${jetty.version}</version>
62-
<type>pom</type>
63-
<scope>import</scope>
64-
</dependency>
65-
<dependency>
66-
<groupId>org.eclipse.jetty.ee9</groupId>
67-
<artifactId>jetty-ee9-bom</artifactId>
68-
<version>${jetty.version}</version>
69-
<type>pom</type>
70-
<scope>import</scope>
71-
</dependency>
7258
<dependency>
7359
<groupId>org.ow2.asm</groupId>
7460
<artifactId>asm-bom</artifactId>
@@ -187,55 +173,6 @@
187173
<artifactId>plexus-xml</artifactId>
188174
<version>3.0.2</version>
189175
</dependency>
190-
<dependency>
191-
<groupId>org.eclipse.jetty</groupId>
192-
<artifactId>jetty-maven</artifactId>
193-
<version>${jetty.version}</version>
194-
<exclusions>
195-
<!-- Pulls in conflicting versions of Apache Commons Logging and Apache HttpComponents Client 4 -->
196-
<exclusion>
197-
<groupId>org.apache.maven.plugin-tools</groupId>
198-
<artifactId>maven-plugin-tools-api</artifactId>
199-
</exclusion>
200-
<exclusion>
201-
<groupId>org.codehaus.plexus</groupId>
202-
<artifactId>plexus-xml</artifactId>
203-
</exclusion>
204-
</exclusions>
205-
</dependency>
206-
<dependency>
207-
<groupId>org.eclipse.jetty</groupId>
208-
<artifactId>jetty-server</artifactId>
209-
</dependency>
210-
<dependency>
211-
<groupId>org.eclipse.jetty.ee9</groupId>
212-
<artifactId>jetty-ee9-maven-plugin</artifactId>
213-
<version>${jetty.version}</version>
214-
<exclusions>
215-
<!-- Pulls in conflicting versions of Apache Commons Logging and Apache HttpComponents Client 4 -->
216-
<exclusion>
217-
<groupId>org.apache.maven.plugin-tools</groupId>
218-
<artifactId>maven-plugin-tools-api</artifactId>
219-
</exclusion>
220-
<exclusion>
221-
<groupId>org.codehaus.plexus</groupId>
222-
<artifactId>plexus-xml</artifactId>
223-
</exclusion>
224-
</exclusions>
225-
</dependency>
226-
<dependency>
227-
<groupId>org.eclipse.jetty.ee9</groupId>
228-
<artifactId>jetty-ee9-servlet</artifactId>
229-
</dependency>
230-
<dependency>
231-
<groupId>org.eclipse.jetty.ee9</groupId>
232-
<artifactId>jetty-ee9-webapp</artifactId>
233-
</dependency>
234-
<dependency>
235-
<groupId>org.eclipse.jetty.ee9.websocket</groupId>
236-
<artifactId>jetty-ee9-websocket-jetty-server</artifactId>
237-
<!-- or jetty-ee9-websocket-jakarta-server -->
238-
</dependency>
239176
<dependency>
240177
<groupId>org.jenkins-ci</groupId>
241178
<artifactId>version-number</artifactId>

src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collections;
3131
import java.util.EnumSet;
3232
import java.util.HashSet;
33+
import java.util.LinkedHashSet;
3334
import java.util.List;
3435
import java.util.Objects;
3536
import java.util.Set;
@@ -38,13 +39,19 @@
3839
import net.java.sezpoz.Index;
3940
import net.java.sezpoz.IndexItem;
4041
import org.apache.commons.io.IOUtils;
42+
import org.apache.maven.RepositoryUtils;
4143
import org.apache.maven.artifact.Artifact;
4244
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
4345
import org.apache.maven.model.Resource;
4446
import org.apache.maven.plugin.MojoExecutionException;
4547
import org.apache.maven.plugins.annotations.Component;
4648
import org.apache.maven.plugins.annotations.Parameter;
49+
import org.apache.maven.project.DefaultDependencyResolutionRequest;
50+
import org.apache.maven.project.DependencyResolutionException;
51+
import org.apache.maven.project.DependencyResolutionRequest;
52+
import org.apache.maven.project.DependencyResolutionResult;
4753
import org.apache.maven.project.MavenProject;
54+
import org.apache.maven.project.ProjectDependenciesResolver;
4855
import org.apache.maven.shared.filtering.MavenFilteringException;
4956
import org.apache.maven.shared.filtering.MavenResourcesExecution;
5057
import org.apache.maven.shared.filtering.MavenResourcesFiltering;
@@ -55,6 +62,8 @@
5562
import org.codehaus.plexus.util.DirectoryScanner;
5663
import org.codehaus.plexus.util.FileUtils;
5764
import org.codehaus.plexus.util.StringUtils;
65+
import org.eclipse.aether.graph.DependencyFilter;
66+
import org.eclipse.aether.util.filter.ScopeDependencyFilter;
5867
import org.jenkinsci.maven.plugins.hpi.util.Utils;
5968

6069
public abstract class AbstractHpiMojo extends AbstractJenkinsMojo {
@@ -162,6 +171,9 @@ public abstract class AbstractHpiMojo extends AbstractJenkinsMojo {
162171
@Component
163172
protected ArchiverManager archiverManager;
164173

174+
@Component
175+
protected ProjectDependenciesResolver dependenciesResolver;
176+
165177
private static final String WEB_INF = "WEB-INF";
166178

167179
private static final String META_INF = "META-INF";
@@ -352,7 +364,7 @@ public void buildExplodedWebapp(File webappDirectory, File jarFile) throws MojoE
352364

353365
try {
354366
List<Resource> webResources = this.webResources != null ? List.of(this.webResources) : null;
355-
if (webResources != null && webResources.size() > 0) {
367+
if (webResources != null && !webResources.isEmpty()) {
356368
copyResourcesWithFiltering(webResources, webappDirectory);
357369
}
358370

@@ -575,7 +587,7 @@ public void buildWebapp(MavenProject project, File webappDirectory) throws MojoE
575587
}
576588
}
577589

578-
if (dependentWarDirectories.size() > 0) {
590+
if (!dependentWarDirectories.isEmpty()) {
579591
getLog().info("Overlaying " + dependentWarDirectories.size() + " war(s).");
580592

581593
// overlay dependent wars
@@ -788,4 +800,44 @@ protected Boolean isSupportDynamicLoading() throws IOException {
788800
return Boolean.TRUE;
789801
}
790802
}
803+
804+
/**
805+
* Performs the equivalent of "@requiresDependencyResolution" mojo attribute,
806+
* so that we can choose the scope at runtime.
807+
*/
808+
protected Set<Artifact> resolveDependencies(String scope) throws MojoExecutionException {
809+
try {
810+
DependencyResolutionRequest request =
811+
new DefaultDependencyResolutionRequest(project, session.getRepositorySession());
812+
request.setResolutionFilter(getDependencyFilter(scope));
813+
DependencyResolutionResult result = dependenciesResolver.resolve(request);
814+
815+
Set<Artifact> artifacts = new LinkedHashSet<>();
816+
if (result.getDependencyGraph() != null
817+
&& !result.getDependencyGraph().getChildren().isEmpty()) {
818+
RepositoryUtils.toArtifacts(
819+
artifacts,
820+
result.getDependencyGraph().getChildren(),
821+
List.of(project.getArtifact().getId()),
822+
request.getResolutionFilter());
823+
}
824+
return artifacts;
825+
} catch (DependencyResolutionException e) {
826+
throw new MojoExecutionException("Unable to resolve dependencies", e);
827+
}
828+
}
829+
830+
/**
831+
* Returns all the transitive plugin dependencies as MavenArtifact.
832+
*/
833+
protected Set<MavenArtifact> getProjectArtifacts() {
834+
return wrap(Artifacts.of(project));
835+
}
836+
837+
protected DependencyFilter getDependencyFilter(String scope) {
838+
if (scope == null || scope.isEmpty()) {
839+
return null;
840+
}
841+
return new ScopeDependencyFilter(null, scope);
842+
}
791843
}

src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsMojo.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package org.jenkinsci.maven.plugins.hpi;
22

3+
import edu.umd.cs.findbugs.annotations.CheckForNull;
4+
import edu.umd.cs.findbugs.annotations.NonNull;
35
import hudson.util.VersionNumber;
46
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
57
import java.io.DataInputStream;
68
import java.io.File;
79
import java.io.IOException;
810
import java.io.InputStream;
11+
import java.util.ArrayList;
12+
import java.util.List;
913
import java.util.jar.JarFile;
14+
import java.util.jar.Manifest;
1015
import java.util.zip.ZipEntry;
1116
import org.apache.maven.artifact.Artifact;
1217
import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -136,6 +141,44 @@ private Artifact resolveJenkinsCore() throws MojoExecutionException {
136141
return MavenArtifact.resolveArtifact(artifact, project, session, repositorySystem);
137142
}
138143

144+
protected void setAddOpensProperty(Artifact artifact) throws MojoExecutionException {
145+
String manifestEntry = getManifestEntry(wrap(artifact));
146+
if (manifestEntry == null) {
147+
getLog().warn("Add-Opens missing from MANIFEST.MF");
148+
return;
149+
}
150+
151+
String argLine = buildArgLine(manifestEntry);
152+
getLog().info("Setting jenkins.addOpens to " + argLine);
153+
project.getProperties().setProperty("jenkins.addOpens", argLine);
154+
}
155+
156+
@NonNull
157+
private static String buildArgLine(String manifestEntry) {
158+
List<String> arguments = new ArrayList<>();
159+
for (String module : manifestEntry.split("\\s+")) {
160+
if (!module.isEmpty()) {
161+
arguments.add("--add-opens");
162+
arguments.add(module + "=ALL-UNNAMED");
163+
}
164+
}
165+
return String.join(" ", arguments);
166+
}
167+
168+
@CheckForNull
169+
private static String getManifestEntry(MavenArtifact artifact) throws MojoExecutionException {
170+
File war = artifact.getFile();
171+
try (JarFile jarFile = new JarFile(war)) {
172+
Manifest manifest = jarFile.getManifest();
173+
if (manifest == null) {
174+
throw new MojoExecutionException("No manifest found in " + war);
175+
}
176+
return manifest.getMainAttributes().getValue("Add-Opens");
177+
} catch (IOException e) {
178+
throw new MojoExecutionException("Failed to read MANIFEST.MF from " + war, e);
179+
}
180+
}
181+
139182
protected MavenArtifact wrap(Artifact a) {
140183
return new MavenArtifact(a, repositorySystem, artifactFactory, projectBuilder, session, project);
141184
}

0 commit comments

Comments
 (0)