Skip to content

Commit

Permalink
Merge pull request #378 from jenkinsci/maven-opts
Browse files Browse the repository at this point in the history
Allowing arbitrary Maven options to be passed
  • Loading branch information
jglick authored Aug 7, 2022
2 parents 6cbe8f5 + a889cc1 commit 519c091
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import hudson.util.VersionNumber;
import java.util.Collections;
import javax.annotation.Nonnull;

/**
* POJO containing CLI arguments & help.
Expand Down Expand Up @@ -137,6 +138,9 @@ public class CliOptions {
"These options will be used a la -D")
private String mavenPropertiesFile;

@Parameter(names="-mavenOptions", description = "Options to pass to Maven (like -Pxxx; not to be confused with Java options, nor Maven properties).")
private List<String> mavenOptions;

@Parameter(names="-hookPrefixes", description = "Prefixes of the extra hooks' classes")
private String hookPrefixes;

Expand Down Expand Up @@ -228,6 +232,11 @@ public String getMavenPropertiesFile() {
return mavenPropertiesFile;
}

@Nonnull
public List<String> getMavenOptions() {
return mavenOptions != null ? Collections.unmodifiableList(mavenOptions) : Collections.emptyList();
}

public String getCacheThresholdStatus() {
return cacheThresholdStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,25 @@ public static void main(String[] args) throws IOException, PlexusContainerExcept
}
}

// We may need this data even in the -war mode
if (updateCenterUrl == null) {
updateCenterUrl = PluginCompatTesterConfig.DEFAULT_UPDATE_CENTER_URL;
PluginCompatTesterConfig config = new PluginCompatTesterConfig();

if (updateCenterUrl != null) {
config.setUpdateCenterUrl(updateCenterUrl);
}
if (parentCoordinates == null) {
parentCoordinates = PluginCompatTesterConfig.DEFAULT_PARENT_GAV;
config.setWorkDirectory(options.getWorkDirectory());
config.setReportFile(reportFile);
config.setM2SettingsFile(options.getM2SettingsFile());

if (parentCoordinates != null && !parentCoordinates.isEmpty()) {
String[] gavChunks = parentCoordinates.split(":");
assert gavChunks.length == 3 || gavChunks.length == 2;
config.setParentGroupId(gavChunks[0]);
config.setParentArtifactId(gavChunks[1]);
if (gavChunks.length == 3 && !"".equals(gavChunks[2])) {
config.setParentVersion(gavChunks[2]);
}
}

PluginCompatTesterConfig config = new PluginCompatTesterConfig(updateCenterUrl, parentCoordinates,
options.getWorkDirectory(), reportFile, options.getM2SettingsFile());
config.setWar(war);
config.setBom(options.getBOM());

Expand Down Expand Up @@ -176,6 +185,7 @@ public static void main(String[] args) throws IOException, PlexusContainerExcept
config.setMavenPropertiesFiles(options.getMavenPropertiesFile());
}

config.setMavenOptions(options.getMavenOptions());

PluginCompatTester tester = new PluginCompatTester(config);
tester.testPlugins();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,29 @@ public class PluginCompatTesterConfig {

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

public static final String DEFAULT_UPDATE_CENTER_URL = "https://updates.jenkins.io/current/update-center.json";
public static final String DEFAULT_PARENT_GROUP = "org.jenkins-ci.plugins";
public static final String DEFAULT_PARENT_ARTIFACT = "plugin";
public static final String DEFAULT_PARENT_GAV = DEFAULT_PARENT_GROUP + ":" + DEFAULT_PARENT_ARTIFACT;

// Update center used to retrieve plugins informations
public final String updateCenterUrl;
public String updateCenterUrl = "https://updates.jenkins.io/current/update-center.json";

// A working directory where the tested plugin's sources will be checked out
public final File workDirectory;
public File workDirectory;

// A report file where will be generated testing report
// If the file already exist, testing report will be merged into it
public final File reportFile;
public File reportFile;

// Path for maven settings file where repository will be provided allowing to
// download jenkins-core artifact (and dependencies)
private final File m2SettingsFile;
private File m2SettingsFile;

// GroupId which will be used to replace tested plugin's parent groupId
// If null, every recorded core coordinates (in report xml) will be played
private String parentGroupId = null;
private String parentGroupId = DEFAULT_PARENT_GROUP;
// ArtifactId which will be used to replace tested plugin's parent artifactId
// If null, every recorded core coordinates (in report xml) will be played
private String parentArtifactId = null;
private String parentArtifactId = DEFAULT_PARENT_ARTIFACT;
// Version which will be used to replace tested plugin's parent version
// If null, latest core version (retrieved via the update center) will be used
private String parentVersion = null;
Expand Down Expand Up @@ -130,6 +128,8 @@ public class PluginCompatTesterConfig {

private Map<String, String> mavenProperties = Collections.emptyMap();
private String mavenPropertiesFile;

private List<String> mavenOptions = Collections.emptyList();

// Classpath prefixes of the extra hooks
private List<String> hookPrefixes = new ArrayList<>(Collections.singletonList("org.jenkins"));
Expand All @@ -151,28 +151,42 @@ public class PluginCompatTesterConfig {
// Flag to indicate if we want to store all the tests names or only failed ones on PCT report files
private boolean storeAll;

/**
* @deprecated just for tests; use {@link #PluginCompatTesterConfig()} and call whatever setters are actually required
*/
@Deprecated
public PluginCompatTesterConfig(File workDirectory, File reportFile, File m2SettingsFile){
this(DEFAULT_UPDATE_CENTER_URL, DEFAULT_PARENT_GAV,
workDirectory, reportFile, m2SettingsFile);
setWorkDirectory(workDirectory);
setReportFile(reportFile);
setM2SettingsFile(m2SettingsFile);
}

public PluginCompatTesterConfig(String updateCenterUrl, String parentGAV,
File workDirectory, File reportFile, File m2SettingsFile){
public PluginCompatTesterConfig() {}

public void setUpdateCenterUrl(String updateCenterUrl) {
this.updateCenterUrl = updateCenterUrl;
if(parentGAV != null && !"".equals(parentGAV)){
String[] gavChunks = parentGAV.split(":");
assert gavChunks.length == 3 || gavChunks.length == 2;
this.parentGroupId = gavChunks[0];
this.parentArtifactId = gavChunks[1];
if(gavChunks.length == 3 && !"".equals(gavChunks[2])){
this.setParentVersion(gavChunks[2]);
}
}
}

public void setWorkDirectory(File workDirectory) {
this.workDirectory = workDirectory;
}

public void setReportFile(File reportFile) {
this.reportFile = reportFile;
}

public void setM2SettingsFile(File m2SettingsFile) {
this.m2SettingsFile = m2SettingsFile;
}

public void setParentGroupId(String parentGroupId) {
this.parentGroupId = parentGroupId;
}

public void setParentArtifactId(String parentArtifactId) {
this.parentArtifactId = parentArtifactId;
}

public String getParentVersion() {
return parentVersion;
}
Expand Down Expand Up @@ -288,6 +302,14 @@ public void setMavenPropertiesFiles( String mavenPropertiesFile ) {
this.mavenPropertiesFile = mavenPropertiesFile;
}

public List<String> getMavenOptions() {
return mavenOptions;
}

public void setMavenOptions(List<String> mavenOptions) {
this.mavenOptions = mavenOptions;
}

@CheckForNull
public File getBom() {
return bom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,9 @@ public PluginCompatReport testPlugins()

SortedSet<MavenCoordinates> testedCores = config.getWar() == null ? generateCoreCoordinatesToTest(data, report) : coreVersionFromWAR(data);

MavenRunner.Config mconfig = new MavenRunner.Config();
mconfig.userSettingsFile = config.getM2SettingsFile();
MavenRunner.Config mconfig = new MavenRunner.Config(config);
// TODO REMOVE
mconfig.userProperties.put( "failIfNoTests", "false" );
mconfig.userProperties.putAll(this.config.retrieveMavenProperties());
report.setTestJavaVersion(config.getTestJavaVersion());

boolean failed = false;
Expand Down Expand Up @@ -791,11 +789,8 @@ private UpdateSite.Data scanBom(HashMap<String, String> pluginGroupIds, String p
private List<File> getBomEntries() throws IOException, XmlPullParserException, PomExecutionException {
File fullDepPom = new MavenBom(config.getBom()).writeFullDepPom(config.workDirectory);

MavenRunner.Config mconfig = new MavenRunner.Config();
mconfig.userSettingsFile = config.getM2SettingsFile();
MavenRunner.Config mconfig = new MavenRunner.Config(config);
System.out.println(mconfig.userSettingsFile);
// TODO REMOVE
mconfig.userProperties.putAll(this.config.retrieveMavenProperties());

File buildLogFile = new File(config.workDirectory
+ File.separator + "bom-download.log");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ private void copy(Path eslintrc, File pluginFolder) {
}

private MavenRunner.Config getMavenConfig(PluginCompatTesterConfig config) throws IOException {
MavenRunner.Config mconfig = new MavenRunner.Config();
mconfig.userSettingsFile = config.getM2SettingsFile();
MavenRunner.Config mconfig = new MavenRunner.Config(config);
// TODO REMOVE
mconfig.userProperties.put("failIfNoTests", "false");
mconfig.userProperties.putAll(config.retrieveMavenProperties());

return mconfig;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Map<String, Object> action(Map<String, Object> moreInfo) throws Exception
PluginCompatTesterConfig config = (PluginCompatTesterConfig) moreInfo.get("config");
boolean shouldExecuteHook = (config.getIncludePlugins().contains("sse-gateway") || (config.getIncludePlugins().contains("workflow-cps")));
runner = new ExternalMavenRunner(config.getExternalMaven());
mavenConfig = getMavenConfig(config);
mavenConfig = new MavenRunner.Config(config);

if (shouldExecuteHook) {
File pluginDir = (File) moreInfo.get("pluginDir");
Expand Down Expand Up @@ -63,10 +63,4 @@ private void removeNodeFolders(File path) throws IOException {
}
}

private MavenRunner.Config getMavenConfig(PluginCompatTesterConfig config) throws IOException {
MavenRunner.Config mconfig = new MavenRunner.Config();
mconfig.userSettingsFile = config.getM2SettingsFile();
mconfig.userProperties.putAll(config.retrieveMavenProperties());
return mconfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void run(Config config, File baseDirectory, File buildLogFile, String...
for (Map.Entry<String, String> entry : config.userProperties.entrySet()) {
cmd.add("--define=" + entry);
}
cmd.addAll(config.mavenOptions);
cmd.addAll(Arrays.asList(goals));
System.out.println("running " + cmd + " in " + baseDirectory + " >> " + buildLogFile);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
package org.jenkins.tools.test.maven;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.jenkins.tools.test.exception.PomExecutionException;
import org.jenkins.tools.test.model.PluginCompatTesterConfig;

public interface MavenRunner {

void run(Config config, File baseDirectory, File buildLogFile, String... goals) throws PomExecutionException;

class Config {
public File userSettingsFile;

public final File userSettingsFile;

public final Map<String,String> userProperties = new TreeMap<>();

public final List<String> mavenOptions;

public Config(PluginCompatTesterConfig pctConfig) throws IOException {
userSettingsFile = pctConfig.getM2SettingsFile();
userProperties.putAll(pctConfig.retrieveMavenProperties());
mavenOptions = pctConfig.getMavenOptions();
}

}

}

0 comments on commit 519c091

Please sign in to comment.