Merge branch 'master' of github.com:spring-projects/sts4

This commit is contained in:
Kris De Volder
2016-12-08 13:54:01 -08:00
9 changed files with 439 additions and 48 deletions

View File

@@ -0,0 +1,27 @@
package org.springframework.ide.vscode.commons.maven;
public class DefaultMavenConfiguration implements IMavenConfiguration {
private String userSettingsFile = null;
private String globalSettingsFile = null;
public void setUserSettingsFile(String userSettingsFile) {
this.userSettingsFile = userSettingsFile;
}
public void setGlobalSettingsFile(String globalSettingsFile) {
this.globalSettingsFile = globalSettingsFile;
}
@Override
public String getUserSettingsFile() {
return userSettingsFile;
}
@Override
public String getGlobalSettingsFile() {
return globalSettingsFile;
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.ide.vscode.commons.maven;
public interface IMavenConfiguration {
public static IMavenConfiguration DEFAULT = new DefaultMavenConfiguration();
String getUserSettingsFile();
String getGlobalSettingsFile();
}

View File

@@ -124,7 +124,7 @@ class MavenBridge {
private DefaultPlexusContainer plexus;
// private final IMavenConfiguration mavenConfiguration;
private final IMavenConfiguration mavenConfiguration;
/**
* Cached parsed settings.xml instance
@@ -137,8 +137,8 @@ class MavenBridge {
/** Last modified timestamp of cached user settings */
private long settings_timestamp;
public MavenBridge(/* IMavenConfiguration mavenConfiguration */) {
// this.mavenConfiguration = mavenConfiguration;
public MavenBridge(IMavenConfiguration mavenConfiguration) {
this.mavenConfiguration = mavenConfiguration;
}
/* package */@SuppressWarnings("deprecation")
@@ -151,17 +151,15 @@ class MavenBridge {
// workspace
// request.setStartTime( new Date() );
// if(mavenConfiguration.getGlobalSettingsFile() != null) {
// request.setGlobalSettingsFile(new
// File(mavenConfiguration.getGlobalSettingsFile()));
// }
if (mavenConfiguration.getGlobalSettingsFile() != null) {
request.setGlobalSettingsFile(new File(mavenConfiguration.getGlobalSettingsFile()));
}
File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
// if(mavenConfiguration.getUserSettingsFile() != null) {
// userSettingsFile = new
// File(mavenConfiguration.getUserSettingsFile());
// }
if (mavenConfiguration.getUserSettingsFile() != null) {
userSettingsFile = new File(mavenConfiguration.getUserSettingsFile());
}
request.setUserSettingsFile(userSettingsFile);
try {
@@ -247,10 +245,9 @@ class MavenBridge {
// MUST NOT use createRequest!
File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
// if(mavenConfiguration.getUserSettingsFile() != null) {
// userSettingsFile = new
// File(mavenConfiguration.getUserSettingsFile());
// }
if (mavenConfiguration.getUserSettingsFile() != null) {
userSettingsFile = new File(mavenConfiguration.getUserSettingsFile());
}
boolean reload = force_reload || settings == null;
@@ -266,10 +263,9 @@ class MavenBridge {
Properties systemProperties = new Properties();
copyProperties(systemProperties, System.getProperties());
request.setSystemProperties(systemProperties);
// if(mavenConfiguration.getGlobalSettingsFile() != null) {
// request.setGlobalSettingsFile(new
// File(mavenConfiguration.getGlobalSettingsFile()));
// }
if (mavenConfiguration.getGlobalSettingsFile() != null) {
request.setGlobalSettingsFile(new File(mavenConfiguration.getGlobalSettingsFile()));
}
if (userSettingsFile != null) {
request.setUserSettingsFile(userSettingsFile);
}

View File

@@ -20,6 +20,7 @@ import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -40,6 +41,7 @@ import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;
import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
@@ -78,9 +80,9 @@ public class MavenCore {
public static final String CLASSPATH_TXT = "classpath.txt";
public static final String POM_XML = "pom.xml";
private static MavenCore instance = null;
private static MavenCore defaultInstance = null;
private MavenBridge maven = new MavenBridge();
private MavenBridge maven;
private Supplier<JandexIndex> javaCoreIndex = Suppliers.memoize(() -> {
try {
@@ -102,11 +104,15 @@ public class MavenCore {
}
});
public static MavenCore getInstance() {
if (instance == null) {
instance = new MavenCore();
public static MavenCore getDefault() {
if (defaultInstance == null) {
defaultInstance = new MavenCore(IMavenConfiguration.DEFAULT);
}
return instance;
return defaultInstance;
}
public MavenCore(IMavenConfiguration config) {
this.maven = new MavenBridge(config);
}
/**
@@ -212,28 +218,55 @@ public class MavenCore {
* @throws MavenException
*/
public Set<Artifact> resolveDependencies(MavenProject project, String scope) throws MavenException {
Set<Artifact> artifacts = new LinkedHashSet<>();
MavenExecutionRequest request = maven.createExecutionRequest();
DefaultRepositorySystemSession session = maven.createRepositorySession(request);
DependencyNode graph = readDependencyTree(maven.lookupComponent(org.eclipse.aether.RepositorySystem.class), session, project, scope);
if (graph != null) {
RepositoryUtils.toArtifacts(artifacts, graph.getChildren(),
Collections.singletonList(project.getArtifact().getId()), null);
// Maven 2.x quirk: an artifact always points at the local repo,
// regardless whether resolved or not
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
for (Artifact artifact : artifacts) {
if (!artifact.isResolved()) {
String path = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact));
artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
ArrayList<DependencyNode> dependencyNodes = new ArrayList<>();
graph.accept(new DependencyVisitor() {
public boolean visitEnter(DependencyNode node) {
if (node.getDependency() != null) {
dependencyNodes.add(node);
}
return true;
}
}
public boolean visitLeave(DependencyNode dependencynode) {
return true;
}
});
LinkedHashSet<Artifact> artifacts = new LinkedHashSet<>();
RepositoryUtils.toArtifacts(artifacts, dependencyNodes,
Collections.singletonList(project.getArtifact().getId()), null);
return artifacts.parallelStream().map(artifact -> {
if (!artifact.isResolved()) {
try {
artifact = maven.resolve(artifact, null, request);
} catch (MavenException e) {
Log.log(e);
// Maven 2.x quirk: an artifact always points at the local repo,
// regardless whether resolved or not
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
String path = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact));
artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
}
}
return artifact;
}).collect(Collectors.toSet());
}
return artifacts;
return Collections.emptySet();
}
public File localRepositoryFolder() throws MavenException {
MavenExecutionRequest request = maven.createExecutionRequest();
DefaultRepositorySystemSession session = maven.createRepositorySession(request);
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
return lrm.getRepository().getBasedir();
}
public Artifact getSources(Artifact artifact) throws MavenException {

View File

@@ -36,7 +36,7 @@ public class MavenJavaProject implements IJavaProject {
private MavenCore maven;
public MavenJavaProject(File pom) throws Exception {
this.maven = MavenCore.getInstance();
this.maven = MavenCore.getDefault();
this.mavenProject = maven.readProject(pom);
this.classpath = new MavenProjectClasspath(mavenProject, maven);
}

View File

@@ -61,7 +61,7 @@ public class MavenProjectClasspath implements IClasspath {
private Supplier<JandexIndex> javaIndex;
public MavenProjectClasspath(MavenProject project) {
this(project, MavenCore.getInstance());
this(project, MavenCore.getDefault());
}
MavenProjectClasspath(MavenProject project, MavenCore maven) {

View File

@@ -11,12 +11,22 @@ package org.springframework.ide.vscode.commons.maven;
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
@@ -32,10 +42,10 @@ public class DependencyTreeTest {
Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/" + projectName).toURI());
MavenBuilder.newBuilder(testProjectPath).clean().pack().skipTests().execute();
MavenProject project = MavenCore.getInstance().readProject(testProjectPath.resolve(MavenCore.POM_XML).toFile());
Set<Path> calculatedClassPath = MavenCore.getInstance().resolveDependencies(project, null).stream().map(artifact -> {
MavenProject project = MavenCore.getDefault().readProject(testProjectPath.resolve(MavenCore.POM_XML).toFile());
Set<Path> calculatedClassPath = MavenCore.getDefault().resolveDependencies(project, null).stream().map(artifact -> {
return Paths.get(artifact.getFile().toURI());
}).collect(Collectors.toSet());;
}).collect(Collectors.toSet());
Set<Path> expectedClasspath = MavenCore.readClassPathFile(testProjectPath.resolve(MavenCore.CLASSPATH_TXT)).collect(Collectors.toSet());
assertEquals(expectedClasspath, calculatedClassPath);
@@ -51,4 +61,65 @@ public class DependencyTreeTest {
testMavenClasspath("gs-rest-service-cors-boot-1.4.1-with-classpath-file");
}
@Test
public void dowloadDependenciesTest() throws Exception {
String userSettingsFile = Paths.get(getClass().getResource("/maven-config/settings.xml").toURI()).toFile().toString();
DefaultMavenConfiguration mavenConfig = new DefaultMavenConfiguration();
mavenConfig.setUserSettingsFile(userSettingsFile);
MavenCore maven = new MavenCore(mavenConfig);
File localRepoFolder = maven.localRepositoryFolder();
if (localRepoFolder.exists()) {
deleteFolderAndContents(localRepoFolder.toPath());
}
assertFalse(localRepoFolder.exists());
Path testProjectPath = Paths.get(getClass().getResource("/gs-rest-service-cors-boot-1.4.1-with-classpath-file").toURI());
MavenProject project = maven.readProject(testProjectPath.resolve(MavenCore.POM_XML).toFile());
Set<Artifact> calculatedClassPath = maven.resolveDependencies(project, null);
String parentFolderPathStr = localRepoFolder.toString();
for (Artifact artifact : calculatedClassPath) {
assertTrue(artifact.isResolved());
File file = artifact.getFile();
assertNotNull(file);
assertTrue(file.toString().startsWith(parentFolderPathStr));
assertTrue(file.exists());
System.out.println(file);
}
deleteFolderAndContents(localRepoFolder.toPath());
assertFalse(localRepoFolder.exists());
}
private static void deleteFolderAndContents(Path folder) throws IOException {
Files.walkFileTree(folder, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}

View File

@@ -59,7 +59,7 @@ public class JavaIndexTest {
}
private static boolean javaVersionHigherThan(int version) {
String versionStr = MavenCore.getInstance().getJavaRuntimeMinorVersion();
String versionStr = MavenCore.getDefault().getJavaRuntimeMinorVersion();
try {
return versionStr != null && Integer.valueOf(versionStr) > version;
} catch (NumberFormatException e) {
@@ -69,7 +69,7 @@ public class JavaIndexTest {
@Test
public void fuzzySearchNoFilter() throws Exception {
List<Tuple2<IType, Double>> results = MavenCore.getInstance().getJavaIndexForJreLibs()
List<Tuple2<IType, Double>> results = MavenCore.getDefault().getJavaIndexForJreLibs()
.fuzzySearchTypes("util.Map", null)
.collectSortedList((o1, o2) -> o2.getT2().compareTo(o1.getT2()))
.block();
@@ -79,7 +79,7 @@ public class JavaIndexTest {
@Test
public void fuzzySearchWithFilter() throws Exception {
List<Tuple2<IType, Double>> results = MavenCore.getInstance().getJavaIndexForJreLibs()
List<Tuple2<IType, Double>> results = MavenCore.getDefault().getJavaIndexForJreLibs()
.fuzzySearchTypes("util.Map", (type) -> Flags.isPrivate(type.getFlags()))
.collectSortedList((o1, o2) -> o2.getT2().compareTo(o1.getT2()))
.block();
@@ -89,7 +89,7 @@ public class JavaIndexTest {
@Test
public void fuzzySearchPackage() throws Exception {
List<Tuple2<String, Double>> results = MavenCore.getInstance().getJavaIndexForJreLibs()
List<Tuple2<String, Double>> results = MavenCore.getDefault().getJavaIndexForJreLibs()
.fuzzySearchPackages("util")
.collectSortedList((o1, o2) -> o2.getT2().compareTo(o1.getT2()))
.block();

View File

@@ -0,0 +1,253 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.home}/conf/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>${java.io.tmpdir}/test-m2-repository</localRepository>
<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->
<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->
<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>
<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>
<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->
<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>
<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
</mirrors>
<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->
<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>
<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>
<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>