Skip to content

Commit

Permalink
Merge pull request #3 from CloudSlang/master
Browse files Browse the repository at this point in the history
Update local score branch
  • Loading branch information
sekler authored Jul 28, 2016
2 parents 7a3fc55 + daba528 commit ff40f3b
Show file tree
Hide file tree
Showing 47 changed files with 254 additions and 59 deletions.
2 changes: 1 addition & 1 deletion dependency-management/dependency-management-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>dependency-management</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>dependency-management-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public interface MavenConfig {
String MAVEN_ARTIFACT_PROPERTY = "artifact";
String MAVEN_MDEP_OUTPUT_FILE_PROPEPRTY = "mdep.outputFile";
String MAVEN_MDEP_PATH_SEPARATOR_PROPERTY = "mdep.pathSeparator";
String TRANSITIVE_PROPERTY = "transitive";

String getLocalMavenRepoPath();
String getRemoteMavenRepoUrl();
Expand Down
2 changes: 1 addition & 1 deletion dependency-management/dependency-management-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>dependency-management</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>dependency-management-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.annotation.PostConstruct;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -161,8 +174,8 @@ public Set<String> getDependencies(Set<String> resources) {

@SuppressWarnings("ConstantConditions")
private void buildDependencyFile(String[] gav) {
String pomFilePath = getResourceFolderPath(gav) + SEPARATOR + getFileName(gav, MavenConfig.POM_EXTENSION);
downloadArtifactsIfNeeded(pomFilePath, gav);
String pomFilePath = getPomFilePath(gav);
downloadArtifacts(gav);
System.setProperty(MavenConfig.MAVEN_MDEP_OUTPUT_FILE_PROPEPRTY, getPathFileName(gav));
System.setProperty(MavenConfig.MAVEN_MDEP_PATH_SEPARATOR_PROPERTY, PATH_FILE_DELIMITER);
System.setProperty(MavenConfig.MAVEN_CLASSWORLDS_CONF_PROPERTY, System.getProperty(MavenConfig.MAVEN_M2_CONF_PATH));
Expand All @@ -173,7 +186,7 @@ private void buildDependencyFile(String[] gav) {
pomFilePath,
MavenConfig.DEPENDENCY_BUILD_CLASSPATH_COMMAND,
MavenConfig.LOG_FILE_FLAG,
constructGavLogFilePath(gav)
constructGavLogFilePath(gav, "build")
};

try {
Expand All @@ -189,8 +202,13 @@ private void buildDependencyFile(String[] gav) {
appendSelfToPathFile(gav, fileToReturn);
}

private String constructGavLogFilePath(String[] gav) {
return new File(mavenLogFolder, gav[0] + GAV_SEPARATOR + gav[1] + GAV_SEPARATOR + gav[2] + ".log").getAbsolutePath();
private String getPomFilePath(String[] gav) {
return getResourceFolderPath(gav) + SEPARATOR + getFileName(gav, MavenConfig.POM_EXTENSION);
}

private String constructGavLogFilePath(String[] gav, String what) {
return new File(mavenLogFolder, gav[0] + GAV_SEPARATOR + gav[1] + GAV_SEPARATOR + gav[2] + GAV_SEPARATOR +
what + ".log").getAbsolutePath();
}

private void invokeMavenLauncher(String[] args) throws Exception {
Expand All @@ -207,30 +225,65 @@ private void invokeMavenLauncher(String[] args) throws Exception {
}
}

private void downloadArtifactsIfNeeded(String pomFilePath, String[] gav) {
File pomFile = new File(pomFilePath);
if(!pomFile.exists()) {
downloadArtifacts(gav);
}
private void downloadArtifacts(String[] gav) {
getDependencies(gav, false);
getDependencies(gav, true);
}

private void downloadArtifacts(String[] gav) {
System.setProperty(MavenConfig.MAVEN_ARTIFACT_PROPERTY, getResourceString(gav));
private void getDependencies(String[] gav, Boolean transitive) {
System.setProperty(MavenConfig.MAVEN_ARTIFACT_PROPERTY, getResourceString(gav, transitive));
System.setProperty(MavenConfig.MAVEN_CLASSWORLDS_CONF_PROPERTY, System.getProperty(MavenConfig.MAVEN_M2_CONF_PATH));
System.setProperty(MavenConfig.TRANSITIVE_PROPERTY, transitive.toString());
String[] args = new String[]{
MavenConfig.MAVEN_SETTINGS_FILE_FLAG,
System.getProperty(MavenConfig.MAVEN_SETTINGS_PATH),
MavenConfig.DEPENDENCY_GET_COMMAND,
MavenConfig.LOG_FILE_FLAG,
constructGavLogFilePath(gav)
constructGavLogFilePath(gav, "get")
};

try {
invokeMavenLauncher(args);
if(!transitive) {
removeTestScopeDependencies(gav);
}
} catch (Exception e) {
throw new IllegalStateException("Failed to download resources using Maven", e);
} finally {
System.getProperties().remove(MavenConfig.TRANSITIVE_PROPERTY);
}

}

private void removeTestScopeDependencies(String[] gav) {
String pomFilePath = getPomFilePath(gav);
try {
removeByXpathExpression(pomFilePath, "/project/dependencies/dependency[scope[contains(text(), 'test')]]");
removeByXpathExpression(pomFilePath, "/project/dependencyManagement/dependencies/dependency[scope[contains(text(), 'test')]]");
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void removeByXpathExpression(String pomFilePath, String expression) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerException {
File xmlFile = new File(pomFilePath);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList)xpath.compile(expression).
evaluate(doc, XPathConstants.NODESET);

if(nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
node.getParentNode().removeChild(node);
}

Transformer transformer = TransformerFactory.newInstance().newTransformer();
// need to convert to file and then to path to override a problem with spaces
Result output = new StreamResult(new File(pomFilePath).getPath());
Source input = new DOMSource(doc);
transformer.transform(input, output);
}
}

private void appendSelfToPathFile(String[] gav, File pathFile) {
Expand Down Expand Up @@ -259,8 +312,18 @@ public boolean accept(File dir, String name) {
}
}

private String getResourceString(String[] gav) {
return StringUtils.arrayToDelimitedString(gav, GAV_DELIMITER);
private String getResourceString(String[] gav, boolean transitive) {
//if not transitive, use type "pom"
String[] newGav = new String[Math.max(4, gav.length)];
System.arraycopy(gav, 0, newGav, 0, gav.length);
if(!transitive) {
newGav[3] = "pom";
} else {
if(newGav[3] == null) {
newGav[3] = "jar";
}
}
return StringUtils.arrayToDelimitedString(newGav, GAV_DELIMITER);
}

private String getPathFileName(String[] gav) {
Expand All @@ -274,6 +337,9 @@ private String getFileName(String[] gav, String extension) {
private List<String> parse(File file) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
String line = reader.readLine();
if(line.startsWith(PATH_FILE_DELIMITER)) {
line = line.substring(PATH_FILE_DELIMITER.length());
}
String[] paths = line.split(PATH_FILE_DELIMITER);
return Arrays.asList(paths);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ public void testBuildClassPath1() {
Assert.assertTrue("Unexpected returned set", retFiles.containsAll(referenceList) && ret.size() == referenceList.size());
}

@Test
public void testBuildClassPath1_1() {
Assume.assumeTrue(shouldRunMaven);
Set <String> ret = dependencyService.getDependencies(new HashSet<>(Collections.singletonList("groupId1:mvn_artifact1:1.1")));
final List<File> retFiles = new ArrayList<>();
for (String s : ret) {
retFiles.add(new File(s));
}
String basePath = new TestConfig().mavenConfig().getLocalMavenRepoPath();
List<File> referenceList = Collections.singletonList(
new File(basePath + "/groupId1/mvn_artifact1/1.1/mvn_artifact1-1.1.jar"));
Assert.assertTrue("Unexpected returned set", retFiles.containsAll(referenceList) && ret.size() == referenceList.size());
}

@Test
public void testBuildClassPath1_2() {
Assume.assumeTrue(shouldRunMaven);
Set <String> ret = dependencyService.getDependencies(new HashSet<>(Collections.singletonList("groupId1:mvn_artifact1:1.2")));
final List<File> retFiles = new ArrayList<>();
for (String s : ret) {
retFiles.add(new File(s));
}
String basePath = new TestConfig().mavenConfig().getLocalMavenRepoPath();
List<File> referenceList = Collections.singletonList(
new File(basePath + "/groupId1/mvn_artifact1/1.2/mvn_artifact1-1.2.jar"));
Assert.assertTrue("Unexpected returned set", retFiles.containsAll(referenceList) && ret.size() == referenceList.size());
}

@Test
public void testBuildClassPath2() {
Assume.assumeTrue(shouldRunMaven);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>kuku</groupId>
<artifactId>muku</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>aaa.ccc</groupId>
<artifactId>bbb</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId1</groupId>
<artifactId>mvn_artifact1</artifactId>
<version>1.1</version>

<name>Kuku</name>
<description>Kuku muku.</description>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>kuku</groupId>
<artifactId>muku</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>aaa.ccc</groupId>
<artifactId>bbb</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId1</groupId>
<artifactId>mvn_artifact1</artifactId>
<version>1.2</version>

<name>Kuku</name>
<description>Kuku muku.</description>

</project>
2 changes: 1 addition & 1 deletion dependency-management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>score-parent</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>dependency-management</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion engine/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>engine</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>data</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion engine/data/score-data-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<artifactId>data</artifactId>
<groupId>io.cloudslang</groupId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/data/score-data-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<artifactId>data</artifactId>
<groupId>io.cloudslang</groupId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/node/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>engine</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>node</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion engine/node/score-node-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>node</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/node/score-node-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>node</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/orchestrator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>engine</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/orchestrator/score-orchestrator-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>orchestrator</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>

<artifactId>score-orchestrator-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion engine/orchestrator/score-orchestrator-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>orchestrator</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.cloudslang</groupId>
<artifactId>score-parent</artifactId>
<version>0.3.33-SNAPSHOT</version>
<version>0.3.35-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Loading

0 comments on commit ff40f3b

Please sign in to comment.