Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Java 8 + download dependencies from remote repos #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
<version>9</version>
</parent>

<groupId>org.fitnesse.plugins</groupId>
Expand Down Expand Up @@ -35,19 +35,19 @@
<dependency>
<groupId>org.fitnesse</groupId>
<artifactId>fitnesse</artifactId>
<version>20150114</version>
<version>20161106</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.6</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -57,6 +57,11 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aether</artifactId>
<version>0.10.1</version>
</dependency>
</dependencies>

<build>
Expand All @@ -82,8 +87,8 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand All @@ -100,5 +105,10 @@
<email>[email protected]</email>
<name>Arjan Molenaar</name>
</developer>
<developer>
<id>smansri</id>
<email>[email protected]</email>
<name>Seifeddin Mansri</name>
</developer>
</developers>
</project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package fitnesse.wikitext.widgets;

@SuppressWarnings("serial")
public class MavenClasspathExtractionException extends Exception {

public MavenClasspathExtractionException(String message, Throwable cause) {
super(message, cause);
}

public MavenClasspathExtractionException(Throwable cause) {
super(cause);
}
Expand Down
150 changes: 39 additions & 111 deletions src/main/java/fitnesse/wikitext/widgets/MavenClasspathExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import org.apache.maven.DefaultMaven;
import org.apache.maven.Maven;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.cli.MavenCli;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequestPopulationException;
import org.apache.maven.execution.MavenExecutionRequestPopulator;
import org.apache.maven.project.*;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.*;
import org.codehaus.plexus.*;
import org.codehaus.plexus.classworlds.ClassWorld;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
Expand All @@ -33,15 +30,14 @@
public class MavenClasspathExtractor {

public final static String DEFAULT_SCOPE = "test";
public final static String MAVEN_USER_SETTINGS = "maven.user.settings";

private final Logger logger = new ConsoleLoggerManager().getLoggerForComponent("maven-classpath-plugin");

private PlexusContainer plexusContainer;

// Ensure M2_HOME variable is handled in a way similar to the mvn executable (script). To the extend possible.
static {
String m2Home = System.getenv().get("M2_HOME");
final String m2Home = System.getenv().get("M2_HOME");
if (m2Home != null && System.getProperty("maven.home") == null) {
System.setProperty("maven.home", m2Home);
}
Expand All @@ -51,20 +47,17 @@ public MavenClasspathExtractor() throws PlexusContainerException {
plexusContainer = buildPlexusContainer(getClass().getClassLoader(), null);
}

public List<String> extractClasspathEntries(File pomFile) throws MavenClasspathExtractionException {
public List<String> extractClasspathEntries(final File pomFile) throws MavenClasspathExtractionException {
return extractClasspathEntries(pomFile, DEFAULT_SCOPE);
}

public List<String> extractClasspathEntries(File pomFile, String scope) throws MavenClasspathExtractionException {

public List<String> extractClasspathEntries(final File pomFile, final String scope) throws MavenClasspathExtractionException {
try {
MavenExecutionRequest mavenExecutionRequest = mavenConfiguration();
final MavenExecutionRequest mavenExecutionRequest = mavenConfiguration();
mavenExecutionRequest.setBaseDirectory(pomFile.getParentFile());
mavenExecutionRequest.setPom(pomFile);

ProjectBuildingResult projectBuildingResult = buildProject(pomFile, mavenExecutionRequest);

return getClasspathForScope(projectBuildingResult, scope);
return getClasspathForScope(buildProject(pomFile, mavenExecutionRequest), scope);

} catch (ComponentLookupException e) {
throw new MavenClasspathExtractionException(e);
Expand All @@ -75,149 +68,84 @@ public List<String> extractClasspathEntries(File pomFile, String scope) throws M
}
}

private List<String> getClasspathForScope(
ProjectBuildingResult projectBuildingResult, String scope)
throws DependencyResolutionRequiredException {
MavenProject project = projectBuildingResult.getProject();

if ("compile".equalsIgnoreCase(scope)) {
return project.getCompileClasspathElements();
} else if ("runtime".equalsIgnoreCase(scope)) {
return project.getRuntimeClasspathElements();
}
return project.getTestClasspathElements();

}
private List<String> getClasspathForScope(ProjectBuildingResult projectBuildingResult, String scope) throws DependencyResolutionRequiredException {
final MavenProject project = projectBuildingResult.getProject();

if ("compile".equalsIgnoreCase(scope)) {
return project.getCompileClasspathElements();
} else if ("runtime".equalsIgnoreCase(scope)) {
return project.getRuntimeClasspathElements();
}
return project.getTestClasspathElements();
}

// protected for test purposes
protected MavenExecutionRequest mavenConfiguration() throws MavenClasspathExtractionException {
MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();
final MavenExecutionRequest mavenExecutionRequest = new DefaultMavenExecutionRequest();

try {
MavenExecutionRequestPopulator executionRequestPopulator = lookup(MavenExecutionRequestPopulator.class);
MavenExecutionRequestPopulator populator = lookup(MavenExecutionRequestPopulator.class);
final MavenExecutionRequestPopulator executionRequestPopulator = lookup(MavenExecutionRequestPopulator.class);
final MavenExecutionRequestPopulator populator = lookup(MavenExecutionRequestPopulator.class);

mavenExecutionRequest.setInteractiveMode(false);

mavenExecutionRequest.setSystemProperties(System.getProperties());
mavenExecutionRequest.getSystemProperties().putAll(getEnvVars());

Settings settings = getSettings(mavenExecutionRequest);

executionRequestPopulator.populateFromSettings(mavenExecutionRequest, settings);
executionRequestPopulator.populateDefaults(mavenExecutionRequest);
populator.populateDefaults(mavenExecutionRequest);

logger.debug( "Local repository " + mavenExecutionRequest.getLocalRepository());

logger.debug("Local repository " + mavenExecutionRequest.getLocalRepository());
} catch (ComponentLookupException e) {
throw new MavenClasspathExtractionException(e);
} catch (SettingsBuildingException e) {
throw new MavenClasspathExtractionException(e);
} catch (MavenExecutionRequestPopulationException e) {
throw new MavenClasspathExtractionException(e);
}
return mavenExecutionRequest;
}

private Settings getSettings(MavenExecutionRequest mavenExecutionRequest)
throws ComponentLookupException, SettingsBuildingException {

SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();

// TODO: should be configurable by system properties?
final File globalSettingsFile = MavenCli.DEFAULT_GLOBAL_SETTINGS_FILE;

//Allows users to set "maven.user.settings" if they use a custom settings.xml
File userSettingsFile;
if(System.getProperty(MAVEN_USER_SETTINGS) != null) {
userSettingsFile = new File(System.getProperty(MAVEN_USER_SETTINGS));
} else {
userSettingsFile = MavenCli.DEFAULT_USER_SETTINGS_FILE;
}

settingsRequest.setGlobalSettingsFile(globalSettingsFile);
settingsRequest.setUserSettingsFile(userSettingsFile);

settingsRequest.setSystemProperties(mavenExecutionRequest
.getSystemProperties());
settingsRequest.setUserProperties(mavenExecutionRequest
.getUserProperties());

logger.debug("Reading global settings from " + settingsRequest.getGlobalSettingsFile());
logger.debug("Reading user settings from " + settingsRequest.getUserSettingsFile());

SettingsBuilder settingsBuilder = lookup(SettingsBuilder.class);

SettingsBuildingResult settingsResult = settingsBuilder
.build(settingsRequest);

if (!settingsResult.getProblems().isEmpty()) {
logger.warn("");
logger.warn("Some problems were encountered while building the effective settings");

for (SettingsProblem problem : settingsResult.getProblems()) {
logger.warn(problem.getMessage() + " @ "
+ problem.getLocation());
}

logger.warn("");
}

return settingsResult.getEffectiveSettings();
}

private Properties getEnvVars() {
Properties envVars = new Properties();
boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS);
for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
String key = "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH));
final Properties envVars = new Properties();
final boolean caseSensitive = !Os.isFamily(Os.FAMILY_WINDOWS);
for (final Map.Entry<String, String> entry : System.getenv().entrySet()) {
final String key = "env." + (caseSensitive ? entry.getKey() : entry.getKey().toUpperCase(Locale.ENGLISH));
envVars.setProperty(key, entry.getValue());
}
return envVars;
}


public ProjectBuildingResult buildProject(File mavenProject, MavenExecutionRequest mavenExecutionRequest) throws ProjectBuildingException, ComponentLookupException {
ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
ProjectBuildingRequest projectBuildingRequest = mavenExecutionRequest.getProjectBuildingRequest();

RepositorySystemSession repositorySystemSession = buildRepositorySystemSession(mavenExecutionRequest);

projectBuildingRequest.setRepositorySession(repositorySystemSession);

final ProjectBuildingRequest projectBuildingRequest = mavenExecutionRequest.getProjectBuildingRequest();
projectBuildingRequest.setRepositorySession(buildRepositorySystemSession(mavenExecutionRequest));
projectBuildingRequest.setProcessPlugins(false);

projectBuildingRequest.setResolveDependencies(true);

return projectBuilder.build(mavenProject, projectBuildingRequest);
return lookup(ProjectBuilder.class).build(mavenProject, projectBuildingRequest);
}

public <T> T lookup(Class<T> clazz) throws ComponentLookupException {
public <T> T lookup(final Class<T> clazz) throws ComponentLookupException {
return plexusContainer.lookup(clazz);
}

private RepositorySystemSession buildRepositorySystemSession(MavenExecutionRequest mavenExecutionRequest) throws ComponentLookupException {
DefaultMaven defaultMaven = (DefaultMaven) lookup(Maven.class);
return defaultMaven.newRepositorySession(mavenExecutionRequest);
private RepositorySystemSession buildRepositorySystemSession(final MavenExecutionRequest mavenExecutionRequest) throws ComponentLookupException {
return ((DefaultMaven) lookup(Maven.class)).newRepositorySession(mavenExecutionRequest);
}

public static PlexusContainer buildPlexusContainer(ClassLoader mavenClassLoader, ClassLoader parent) throws PlexusContainerException {
DefaultContainerConfiguration conf = new DefaultContainerConfiguration();
public static PlexusContainer buildPlexusContainer(final ClassLoader mavenClassLoader, final ClassLoader parent) throws PlexusContainerException {
final DefaultContainerConfiguration conf = new DefaultContainerConfiguration();

ClassWorld classWorld = new ClassWorld();
final ClassWorld classWorld = new ClassWorld();

ClassRealm classRealm = new ClassRealm( classWorld, "maven", mavenClassLoader );
classRealm.setParentRealm( new ClassRealm( classWorld, "maven-parent",
parent == null ? Thread.currentThread().getContextClassLoader()
: parent ) );
conf.setRealm( classRealm );
final ClassRealm classRealm = new ClassRealm(classWorld, "maven", mavenClassLoader);
final ClassLoader classLoader = parent == null ? Thread.currentThread().getContextClassLoader() : parent;
classRealm.setParentRealm(new ClassRealm(classWorld, "maven-parent", classLoader));
conf.setRealm(classRealm);

return buildPlexusContainer(conf);
}

private static PlexusContainer buildPlexusContainer(ContainerConfiguration containerConfiguration ) throws PlexusContainerException {
DefaultPlexusContainer plexusContainer = new DefaultPlexusContainer( containerConfiguration );
return plexusContainer;
private static PlexusContainer buildPlexusContainer(final ContainerConfiguration containerConfiguration) throws PlexusContainerException {
return new DefaultPlexusContainer(containerConfiguration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class MavenClasspathSymbolType extends SymbolType implements Rule, Transl

private MavenClasspathExtractor mavenClasspathExtractor;

private final Map<String, List<String>> classpathCache = new HashMap<String, List<String>>();
private final Map<String, List<String>> classpathCache = new HashMap<>();

public MavenClasspathSymbolType() throws PlexusContainerException {
super("MavenClasspathSymbolType");

String disablePropertyValue = System.getProperty(DISABLE_KEY);
final String disablePropertyValue = System.getProperty(DISABLE_KEY);
if (!"true".equalsIgnoreCase(disablePropertyValue)) {
this.mavenClasspathExtractor = new MavenClasspathExtractor();
}
Expand All @@ -47,32 +47,27 @@ public MavenClasspathSymbolType() throws PlexusContainerException {

@Override
public String toTarget(Translator translator, Symbol symbol) {
List<String> classpathElements = null;
ParsedSymbol parsedSymbol = getParsedSymbol(translator, symbol);
StringBuilder classpathForRender = new StringBuilder("<p class='meta'>Maven classpath [file: ")
final ParsedSymbol parsedSymbol = getParsedSymbol(translator, symbol);
final StringBuilder classpathForRender = new StringBuilder("<p class='meta'>Maven classpath [file: ")
.append(parsedSymbol.getPomFile())
.append(", scope: ")
.append(parsedSymbol.getScope())
.append("]:</p>")
.append("<ul class='meta'>");
try {
classpathElements = getClasspathElements(parsedSymbol);
for (String element : classpathElements) {
for (final String element : getClasspathElements(parsedSymbol)) {
classpathForRender.append("<li>").append(element).append("</li>");
}
} catch (MavenClasspathExtractionException e) {
classpathForRender.append("<li class='error'>Unable to parse POM file: ")
.append(e.getMessage()).append("</li>");
classpathForRender.append("<li class='error'>Unable to parse POM file: ").append(e.getMessage()).append("</li>");
}

classpathForRender.append("</ul>");
return classpathForRender.toString();

}

@SuppressWarnings("unchecked")
private List<String> getClasspathElements(final ParsedSymbol parsedSymbol) throws MavenClasspathExtractionException {
String symbol = parsedSymbol.symbol;
final String symbol = parsedSymbol.symbol;
if(classpathCache.containsKey(symbol)) {
return classpathCache.get(symbol);
} else {
Expand All @@ -93,7 +88,7 @@ private ParsedSymbol getParsedSymbol(Translator translator, Symbol symbol) {
public Maybe<Symbol> parse(Symbol current, Parser parser) {
if (!parser.isMoveNext(SymbolType.Whitespace)) return Symbol.nothing;

return new Maybe<Symbol>(current.add(parser.parseToEnds(0, SymbolProvider.pathRuleProvider, new SymbolType[] {SymbolType.Newline})));
return new Maybe<>(current.add(parser.parseToEnds(0, SymbolProvider.pathRuleProvider, new SymbolType[] {SymbolType.Newline})));
}

@Override
Expand Down Expand Up @@ -135,7 +130,7 @@ public ParsedSymbol(String symbol) {

private void parseSymbol() {
if (symbol.contains("@")) {
String[] s = symbol.split("@");
final String[] s = symbol.split("@");
pomFile = new File(s[0]);
scope = s[1];
} else {
Expand Down Expand Up @@ -164,7 +159,7 @@ public int hashCode() {
@Override
public boolean equals(Object obj) {
if (obj instanceof ParsedSymbol) {
ParsedSymbol ps = (ParsedSymbol) obj;
final ParsedSymbol ps = (ParsedSymbol) obj;
return symbol.equals(ps.symbol) && lastModified == ps.lastModified;
}
return false;
Expand Down
Loading