Skip to content

Commit

Permalink
[JENKINS-50598] - Add support of passing custom WAR file in WAR explo…
Browse files Browse the repository at this point in the history
…der using system property
  • Loading branch information
oleg-nenashev committed Apr 5, 2018
1 parent 618723e commit e158a39
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/main/java/org/jvnet/hudson/test/WarExploder.java
Expand Up @@ -33,6 +33,8 @@
import java.util.logging.Logger;
import jenkins.model.Jenkins;

import javax.annotation.CheckForNull;

/**
* Ensures that <tt>jenkins.war</tt> is exploded.
*
Expand All @@ -46,6 +48,10 @@ public final class WarExploder {

private static final Logger LOGGER = Logger.getLogger(WarExploder.class.getName());

public static final String JENKINS_WAR_PATH_PROPERTY_NAME = "jth.jenkins-war.path";
@CheckForNull
private static final String JENKINS_WAR_PATH = System.getProperty(JENKINS_WAR_PATH_PROPERTY_NAME);

public static File getExplodedDir() throws Exception {
// rethrow an exception every time someone tries to do this, so that when explode()
// fails, you can see the cause no matter which test case you look at.
Expand All @@ -66,7 +72,7 @@ public static File getExplodedDir() throws Exception {
}

/**
* Explodes hudson.war, if necessary, and returns its root dir.
* Explodes jenkins.war, if necessary, and returns its root dir.
*/
private static File explode() throws Exception {
// are we in the Jenkins main workspace? If so, pick up hudson/main/war/resources
Expand All @@ -84,23 +90,34 @@ private static File explode() throws Exception {
}
}

// locate jenkins.war
File war;
URL winstone = WarExploder.class.getResource("/winstone.jar");
if (winstone != null) {
war = Which.jarFile(Class.forName("executable.Executable"));
final File war;
if (JENKINS_WAR_PATH != null) {
war = new File(JENKINS_WAR_PATH).getAbsoluteFile();
LOGGER.log(Level.INFO, "Using a predefined WAR file {0} define by the -{1} system property",
new Object[] {war, JENKINS_WAR_PATH_PROPERTY_NAME});
if (!war.exists()) {
throw new IOException("A Predefined WAR file path does not exist: " + war);
} else if (!war.isFile()) {
throw new IOException("A Predefined WAR file path does not point to a file: " + war);
}
} else {
// JENKINS-45245: work around incorrect test classpath in IDEA. Note that this will not correctly handle timestamped snapshots; in that case use `mvn test`.
File core = Which.jarFile(Jenkins.class); // will fail with IllegalArgumentException if have neither jenkins-war.war nor jenkins-core.jar in ${java.class.path}
String version = core.getParentFile().getName();
if (core.getName().equals("jenkins-core-" + version + ".jar") && core.getParentFile().getParentFile().getName().equals("jenkins-core")) {
war = new File(new File(new File(core.getParentFile().getParentFile().getParentFile(), "jenkins-war"), version), "jenkins-war-" + version + ".war");
if (!war.isFile()) {
throw new AssertionError(war + " does not yet exist. Prime your development environment by running `mvn validate`.");
}
LOGGER.log(Level.FINE, "{0} is the continuation of the classpath by other means", war);
// locate jenkins.war
URL winstone = WarExploder.class.getResource("/winstone.jar");
if (winstone != null) {
war = Which.jarFile(Class.forName("executable.Executable"));
} else {
throw new AssertionError(core + " is not in the expected location, and jenkins-war-*.war was not in " + System.getProperty("java.class.path"));
// JENKINS-45245: work around incorrect test classpath in IDEA. Note that this will not correctly handle timestamped snapshots; in that case use `mvn test`.
File core = Which.jarFile(Jenkins.class); // will fail with IllegalArgumentException if have neither jenkins-war.war nor jenkins-core.jar in ${java.class.path}
String version = core.getParentFile().getName();
if (core.getName().equals("jenkins-core-" + version + ".jar") && core.getParentFile().getParentFile().getName().equals("jenkins-core")) {
war = new File(new File(new File(core.getParentFile().getParentFile().getParentFile(), "jenkins-war"), version), "jenkins-war-" + version + ".war");
if (!war.isFile()) {
throw new AssertionError(war + " does not yet exist. Prime your development environment by running `mvn validate`.");
}
LOGGER.log(Level.FINE, "{0} is the continuation of the classpath by other means", war);
} else {
throw new AssertionError(core + " is not in the expected location, and jenkins-war-*.war was not in " + System.getProperty("java.class.path"));
}
}
}

Expand Down

0 comments on commit e158a39

Please sign in to comment.