Fix maven build on Windows

This commit is contained in:
Alex Boyko
2016-10-24 12:56:32 -04:00
parent 46e2334b0c
commit ea6079bea2
2 changed files with 156 additions and 150 deletions

View File

@@ -1,75 +1,78 @@
package org.springframework.ide.vscode.commons.maven;
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
/**
* Tests for comparing maven calculated dependencies with ours
*
* @author Alex Boyko
*
*/
public class DependencyTreeTest {
/**
* Build test project if it's not built already
* @throws Exception
*/
private static void buildProject(Path testProjectPath) throws Exception {
if (!Files.exists(testProjectPath.resolve("classpath.txt"))) {
testProjectPath.resolve("mvnw").toFile().setExecutable(true);
ExternalProcess process = new ExternalProcess(testProjectPath.toFile(), new ExternalCommand("./mvnw", "clean", "package"), true);
if (process.getExitValue() != 0) {
throw new RuntimeException("Failed to build test project");
}
}
}
private static Set<Path> readClassPathFile(Path classPathFilePath) throws IOException {
InputStream in = Files.newInputStream(classPathFilePath);
String text = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining());
Path dir = classPathFilePath.getParent();
return Arrays.stream(text.split(File.pathSeparator)).map(dir::resolve).collect(Collectors.toSet());
}
@Test
public void mavenTest() throws Exception {
Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/demo-1").toURI());
buildProject(testProjectPath);
MavenProject project = MavenCore.getInstance().readProject(testProjectPath.resolve("pom.xml").toFile());
Set<Path> calculatedClassPath = MavenCore.getInstance().resolveDependencies(project, null).stream().map(artifact -> {
return Paths.get(artifact.getFile().toURI());
}).collect(Collectors.toSet());;
Set<Path> expectedClasspath = readClassPathFile(testProjectPath.resolve("classpath.txt"));
assertEquals(expectedClasspath, calculatedClassPath);
}
}
package org.springframework.ide.vscode.commons.maven;
/*******************************************************************************
* Copyright (c) 2016 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
/**
* Tests for comparing maven calculated dependencies with ours
*
* @author Alex Boyko
*
*/
public class DependencyTreeTest {
/**
* Build test project if it's not built already
* @throws Exception
*/
private static void buildProject(Path testProjectPath) throws Exception {
if (!Files.exists(testProjectPath.resolve("classpath.txt"))) {
Path mvnwPath = System.getProperty("os.name").toLowerCase().startsWith("win")
? testProjectPath.resolve("mvnw.cmd") : testProjectPath.resolve("mvnw");
mvnwPath.toFile().setExecutable(true);
ExternalProcess process = new ExternalProcess(testProjectPath.toFile(),
new ExternalCommand(mvnwPath.toAbsolutePath().toString(), "clean", "package"), true);
if (process.getExitValue() != 0) {
throw new RuntimeException("Failed to build test project");
}
}
}
private static Set<Path> readClassPathFile(Path classPathFilePath) throws IOException {
InputStream in = Files.newInputStream(classPathFilePath);
String text = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining());
Path dir = classPathFilePath.getParent();
return Arrays.stream(text.split(File.pathSeparator)).map(dir::resolve).collect(Collectors.toSet());
}
@Test
public void mavenTest() throws Exception {
Path testProjectPath = Paths.get(DependencyTreeTest.class.getResource("/demo-1").toURI());
buildProject(testProjectPath);
MavenProject project = MavenCore.getInstance().readProject(testProjectPath.resolve("pom.xml").toFile());
Set<Path> calculatedClassPath = MavenCore.getInstance().resolveDependencies(project, null).stream().map(artifact -> {
return Paths.get(artifact.getFile().toURI());
}).collect(Collectors.toSet());;
Set<Path> expectedClasspath = readClassPathFile(testProjectPath.resolve("classpath.txt"));
assertEquals(expectedClasspath, calculatedClassPath);
}
}

View File

@@ -1,75 +1,78 @@
package org.springframework.ide.vscode.languageserver.testharness;
import static org.junit.Assert.assertTrue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
import org.springframework.ide.vscode.commons.util.HtmlSnippet;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
/**
* Class meant to be used in writing tests that require test projects to be populated
* from some data on the classpath.
*/
public class TestProjectHarness {
private static final class TestProject implements IJavaProject {
private Path location;
public TestProject(Path location) {
this.location = location;
}
@Override
public HtmlSnippet getJavaDoc() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getElementName() {
return location.toFile().getName();
}
@Override
public boolean exists() {
return true;
}
@Override
public IClasspath getClasspath() {
return () -> location;
}
@Override
public IType findType(String fqName) {
// TODO Auto-generated method stub
return null;
}
}
public Cache<String, IJavaProject> cache = CacheBuilder.newBuilder().build();
public IJavaProject mavenProject(String name) throws Exception {
Path testProjectPath = Paths.get(TestProjectHarness.class.getResource("/"+name).toURI());
assertTrue(Files.exists(testProjectPath));
if (!Files.exists(testProjectPath.resolve("classpath.txt"))) {
testProjectPath.resolve("mvnw").toFile().setExecutable(true);
ExternalProcess process = new ExternalProcess(testProjectPath.toFile(), new ExternalCommand("./mvnw", "clean", "package"), true);
if (process.getExitValue() != 0) {
throw new RuntimeException("Failed to build test project");
}
}
return new TestProject(testProjectPath);
}
}
package org.springframework.ide.vscode.languageserver.testharness;
import static org.junit.Assert.assertTrue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.java.IType;
import org.springframework.ide.vscode.commons.util.ExternalCommand;
import org.springframework.ide.vscode.commons.util.ExternalProcess;
import org.springframework.ide.vscode.commons.util.HtmlSnippet;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
/**
* Class meant to be used in writing tests that require test projects to be populated
* from some data on the classpath.
*/
public class TestProjectHarness {
private static final class TestProject implements IJavaProject {
private Path location;
public TestProject(Path location) {
this.location = location;
}
@Override
public HtmlSnippet getJavaDoc() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getElementName() {
return location.toFile().getName();
}
@Override
public boolean exists() {
return true;
}
@Override
public IClasspath getClasspath() {
return () -> location;
}
@Override
public IType findType(String fqName) {
// TODO Auto-generated method stub
return null;
}
}
public Cache<String, IJavaProject> cache = CacheBuilder.newBuilder().build();
public IJavaProject mavenProject(String name) throws Exception {
Path testProjectPath = Paths.get(TestProjectHarness.class.getResource("/" + name).toURI());
assertTrue(Files.exists(testProjectPath));
if (!Files.exists(testProjectPath.resolve("classpath.txt"))) {
Path mvnwPath = System.getProperty("os.name").toLowerCase().startsWith("win")
? testProjectPath.resolve("mvnw.cmd") : testProjectPath.resolve("mvnw");
mvnwPath.toFile().setExecutable(true);
ExternalProcess process = new ExternalProcess(testProjectPath.toFile(),
new ExternalCommand(mvnwPath.toAbsolutePath().toString(), "clean", "package"), true);
if (process.getExitValue() != 0) {
throw new RuntimeException("Failed to build test project");
}
}
return new TestProject(testProjectPath);
}
}