From 75790fed3556602de34f7cab5bca16df5cf39d1b Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 12:04:28 +0200 Subject: [PATCH 01/33] feat(maven): create a maven launcher --- pom.xml | 5 + src/main/java/spoon/MavenLauncher.java | 226 +++++++++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 src/main/java/spoon/MavenLauncher.java diff --git a/pom.xml b/pom.xml index 2cb92bcabda..46a07418626 100644 --- a/pom.xml +++ b/pom.xml @@ -246,6 +246,11 @@ commons-io 2.5 + + org.apache.maven + maven-model + 3.5.0 + org.apache.commons commons-lang3 diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java new file mode 100644 index 00000000000..3d78fa1945f --- /dev/null +++ b/src/main/java/spoon/MavenLauncher.java @@ -0,0 +1,226 @@ +package spoon; + +import org.apache.maven.model.Build; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Create a Spoon launcher from a maven pom file + */ +public class MavenLauncher extends Launcher { + + private String projectRoot; + private String m2RepositoryPath; + private boolean includeTest = false; + + public MavenLauncher(String pomPatch, boolean includeTest) { + this(pomPatch, "/home/thomas/.m2/repository/", includeTest); + } + + /** + * + * @param projectRoot the path to the root of the project (the folder that contains the pom) + * @param m2RepositoryPath the path to the m2repository + */ + public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includeTest) { + this.projectRoot = projectRoot; + this.m2RepositoryPath = m2RepositoryPath; + + if (!new File(projectRoot).isDirectory()) { + throw new SpoonException(projectRoot + " has to be a folder"); + } + + InheritanceModel model = null; + try { + model = readPOM(projectRoot, null); + } catch (Exception e) { + throw new SpoonException("Unable to read the pom", e); + } + + // source + List sourceDirectories = model.getSourceDirectories(); + for (File sourceDirectory : sourceDirectories) { + this.addInputResource(sourceDirectory.getAbsolutePath()); + } + + // test + if (includeTest) { + List testSourceDirectories = model.getTestDirectories(); + for (File sourceDirectory : testSourceDirectories) { + this.addInputResource(sourceDirectory.getAbsolutePath()); + } + } + + // dependencies + List dependencies = model.getDependencies(); + String[] classpath = new String[dependencies.size()]; + for (int i = 0; i < dependencies.size(); i++) { + File file = dependencies.get(i); + classpath[i] = file.getAbsolutePath(); + } + this.getModelBuilder().setSourceClasspath(classpath); + } + + + private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException { + File parentPOM = new File(path + "/pom.xml"); + if (!parentPOM.exists()) { + return null; + } + MavenXpp3Reader pomReader = new MavenXpp3Reader(); + Model model = pomReader.read(new FileReader(parentPOM)); + InheritanceModel inheritanceModel = new InheritanceModel(model, parent, new File(path)); + for (String module : model.getModules()) { + inheritanceModel.addModule(readPOM(path + "/" + module, inheritanceModel)); + } + return inheritanceModel; + } + + class InheritanceModel { + private List modules = new ArrayList<>(); + private Model model; + private InheritanceModel parent; + private File directory; + + public InheritanceModel(Model model, InheritanceModel parent, File directory) { + this.model = model; + this.parent = parent; + this.directory = directory; + } + + public void addModule(InheritanceModel module) { + modules.add(module); + } + + public List getModules() { + return modules; + } + + public Model getModel() { + return model; + } + + public InheritanceModel getParent() { + return parent; + } + + public List getSourceDirectories() { + List output = new ArrayList<>(); + String sourcePath = null; + + Build build = model.getBuild(); + if (build != null) { + sourcePath = build.getSourceDirectory(); + } + if (sourcePath == null) { + sourcePath = directory.getAbsolutePath() + "/src/main/java"; + } + File source = new File(sourcePath); + if (source.exists()) { + output.add(source); + } + for (InheritanceModel module : modules) { + output.addAll(module.getSourceDirectories()); + } + return output; + } + + public List getTestDirectories() { + List output = new ArrayList<>(); + String sourcePath = null; + + Build build = model.getBuild(); + if (build != null) { + sourcePath = build.getTestSourceDirectory(); + } + if (sourcePath == null) { + sourcePath = directory.getAbsolutePath() + "/src/test/java"; + } + File source = new File(sourcePath); + if (source.exists()) { + output.add(source); + } + for (InheritanceModel module : modules) { + output.addAll(module.getTestDirectories()); + } + return output; + } + + public List getDependencies() { + Set output = new HashSet<>(); + + List dependencies = model.getDependencies(); + for (Dependency dependency : dependencies) { + String depPath = m2RepositoryPath; + String groupId = dependency.getGroupId().replace(".", "/"); + depPath += groupId + "/"; + depPath += dependency.getArtifactId() + "/"; + String version = dependency.getVersion(); + if (version.startsWith("$")) { + version = getProperty(version.substring(2, version.length() - 1)); + } + depPath += version + "/"; + + String fileName = dependency.getArtifactId() + "-" + version + ".jar"; + + depPath += fileName; + File jar = new File(depPath); + if (jar.exists()) { + output.add(jar); + } + } + + for (InheritanceModel module : modules) { + output.addAll(module.getDependencies()); + } + return new ArrayList<>(output); + } + + private String getProperty(String key) { + if ("project.version".equals(key)) { + if (model.getVersion() != null) { + return model.getVersion(); + } + } + String value = model.getProperties().getProperty(key); + if (value == null) { + if (parent == null) { + return null; + } + return parent.getProperty(key); + } + return value; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(model.getName()); + if (modules.isEmpty()) { + return sb.toString(); + } + sb.append(" {\n"); + for (int i = 0; i < modules.size(); i++) { + InheritanceModel inheritanceModel = modules.get(i); + String child = inheritanceModel.toString(); + for (String s : child.split("\n")) { + sb.append("\t"); + sb.append(s); + sb.append("\n"); + } + } + sb.append("}"); + return sb.toString(); + } + } +} From 3ec3b2b11713b1cc3f7a850c11361e6c94f22818 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 12:08:55 +0200 Subject: [PATCH 02/33] fix default m2 repository --- src/main/java/spoon/MavenLauncher.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 3d78fa1945f..14fead307ef 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -24,7 +25,7 @@ public class MavenLauncher extends Launcher { private boolean includeTest = false; public MavenLauncher(String pomPatch, boolean includeTest) { - this(pomPatch, "/home/thomas/.m2/repository/", includeTest); + this(pomPatch, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), includeTest); } /** From e40fe6bf40ba1f1747209a5061f184d92ce48f81 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 12:26:35 +0200 Subject: [PATCH 03/33] fix checkstyle and add a test --- src/main/java/spoon/MavenLauncher.java | 21 ++++++--------------- src/test/java/spoon/MavenLauncherTest.java | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 src/test/java/spoon/MavenLauncherTest.java diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 14fead307ef..514cd768808 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -9,6 +9,7 @@ import java.io.File; import java.io.FileReader; import java.io.IOException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; @@ -19,10 +20,7 @@ * Create a Spoon launcher from a maven pom file */ public class MavenLauncher extends Launcher { - - private String projectRoot; private String m2RepositoryPath; - private boolean includeTest = false; public MavenLauncher(String pomPatch, boolean includeTest) { this(pomPatch, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), includeTest); @@ -34,14 +32,13 @@ public MavenLauncher(String pomPatch, boolean includeTest) { * @param m2RepositoryPath the path to the m2repository */ public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includeTest) { - this.projectRoot = projectRoot; this.m2RepositoryPath = m2RepositoryPath; if (!new File(projectRoot).isDirectory()) { throw new SpoonException(projectRoot + " has to be a folder"); } - InheritanceModel model = null; + InheritanceModel model; try { model = readPOM(projectRoot, null); } catch (Exception e) { @@ -93,7 +90,7 @@ class InheritanceModel { private InheritanceModel parent; private File directory; - public InheritanceModel(Model model, InheritanceModel parent, File directory) { + InheritanceModel(Model model, InheritanceModel parent, File directory) { this.model = model; this.parent = parent; this.directory = directory; @@ -124,7 +121,7 @@ public List getSourceDirectories() { sourcePath = build.getSourceDirectory(); } if (sourcePath == null) { - sourcePath = directory.getAbsolutePath() + "/src/main/java"; + sourcePath = Paths.get(directory.getAbsolutePath(), "src", "main", "java").toString(); } File source = new File(sourcePath); if (source.exists()) { @@ -162,20 +159,14 @@ public List getDependencies() { List dependencies = model.getDependencies(); for (Dependency dependency : dependencies) { - String depPath = m2RepositoryPath; String groupId = dependency.getGroupId().replace(".", "/"); - depPath += groupId + "/"; - depPath += dependency.getArtifactId() + "/"; String version = dependency.getVersion(); if (version.startsWith("$")) { version = getProperty(version.substring(2, version.length() - 1)); } - depPath += version + "/"; - String fileName = dependency.getArtifactId() + "-" + version + ".jar"; - - depPath += fileName; - File jar = new File(depPath); + Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version, fileName); + File jar = depPath.toFile(); if (jar.exists()) { output.add(jar); } diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java new file mode 100644 index 00000000000..62d4e426175 --- /dev/null +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -0,0 +1,22 @@ +package spoon; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class MavenLauncherTest { + @Test + public void spoonMavenLauncherTest() { + // without the tests + MavenLauncher launcher = new MavenLauncher("./", false); + assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); + // 54 because of the sub folder + assertEquals(54, launcher.getModelBuilder().getInputSources().size()); + + // with the tests + launcher = new MavenLauncher("./", true); + assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); + // 54 because of the sub folder + assertEquals(235, launcher.getModelBuilder().getInputSources().size()); + } +} From 2ac9e80c3fea4d70704891b79d3b050e0efadad2 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 12:29:46 +0200 Subject: [PATCH 04/33] fix potential issues with path on windows --- src/main/java/spoon/MavenLauncher.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 514cd768808..6873f1def6f 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -44,6 +44,9 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includ } catch (Exception e) { throw new SpoonException("Unable to read the pom", e); } + if (model == null) { + throw new SpoonException(" Unable to create the model, pom not found?"); + } // source List sourceDirectories = model.getSourceDirectories(); @@ -71,7 +74,7 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includ private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException { - File parentPOM = new File(path + "/pom.xml"); + File parentPOM = Paths.get(path, "pom.xml").toFile(); if (!parentPOM.exists()) { return null; } @@ -79,7 +82,7 @@ private InheritanceModel readPOM(String path, InheritanceModel parent) throws IO Model model = pomReader.read(new FileReader(parentPOM)); InheritanceModel inheritanceModel = new InheritanceModel(model, parent, new File(path)); for (String module : model.getModules()) { - inheritanceModel.addModule(readPOM(path + "/" + module, inheritanceModel)); + inheritanceModel.addModule(readPOM(Paths.get(path, module).toString(), inheritanceModel)); } return inheritanceModel; } @@ -100,10 +103,6 @@ public void addModule(InheritanceModel module) { modules.add(module); } - public List getModules() { - return modules; - } - public Model getModel() { return model; } @@ -142,7 +141,7 @@ public List getTestDirectories() { sourcePath = build.getTestSourceDirectory(); } if (sourcePath == null) { - sourcePath = directory.getAbsolutePath() + "/src/test/java"; + sourcePath = Paths.get(directory.getAbsolutePath(), "src", "test", "java").toString(); } File source = new File(sourcePath); if (source.exists()) { From 71dccc8067d90d0dfe3634c4fd3f60791e02e8c0 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 13:06:35 +0200 Subject: [PATCH 05/33] add license --- src/main/java/spoon/MavenLauncher.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 6873f1def6f..7f0664d0f31 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -1,3 +1,19 @@ +/** + * Copyright (C) 2006-2017 INRIA and contributors + * Spoon - http://spoon.gforge.inria.fr/ + * + * This software is governed by the CeCILL-C License under French law and + * abiding by the rules of distribution of free software. You can use, modify + * and/or redistribute the software under the terms of the CeCILL-C license as + * circulated by CEA, CNRS and INRIA at http://www.cecill.info. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-C license and that you accept its terms. + */ package spoon; import org.apache.maven.model.Build; From 373cb4f18bda3c21b1752af1f7531a200277d935 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 14:42:08 +0200 Subject: [PATCH 06/33] uses no classpath when a dependency is missing --- src/main/java/spoon/MavenLauncher.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 7f0664d0f31..799524b49ab 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -184,6 +184,9 @@ public List getDependencies() { File jar = depPath.toFile(); if (jar.exists()) { output.add(jar); + } else { + // if the a dependency is not found, uses the no classpath mode + getEnvironment().setNoClasspath(true); } } From 36817a5db7ad5c426b41ab97cd4db78f0bfa37f6 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 14:43:12 +0200 Subject: [PATCH 07/33] rename variable --- src/main/java/spoon/MavenLauncher.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 799524b49ab..c74ea3ad19b 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -90,12 +90,12 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includ private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException { - File parentPOM = Paths.get(path, "pom.xml").toFile(); - if (!parentPOM.exists()) { + File pomFile = Paths.get(path, "pom.xml").toFile(); + if (!pomFile.exists()) { return null; } MavenXpp3Reader pomReader = new MavenXpp3Reader(); - Model model = pomReader.read(new FileReader(parentPOM)); + Model model = pomReader.read(new FileReader(pomFile)); InheritanceModel inheritanceModel = new InheritanceModel(model, parent, new File(path)); for (String module : model.getModules()) { inheritanceModel.addModule(readPOM(Paths.get(path, module).toString(), inheritanceModel)); From 68a9cd97562ed3d77f0378e317bd86e6981082e9 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 21:03:39 +0200 Subject: [PATCH 08/33] select the type of source you want --- src/main/java/spoon/MavenLauncher.java | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index c74ea3ad19b..e2f7dcf1240 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -37,9 +37,14 @@ */ public class MavenLauncher extends Launcher { private String m2RepositoryPath; + public enum SOURCE_TYPE { + SOURCE, + TEST, + ALL + } - public MavenLauncher(String pomPatch, boolean includeTest) { - this(pomPatch, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), includeTest); + public MavenLauncher(String pomPatch, SOURCE_TYPE sourceType) { + this(pomPatch, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), sourceType); } /** @@ -47,7 +52,7 @@ public MavenLauncher(String pomPatch, boolean includeTest) { * @param projectRoot the path to the root of the project (the folder that contains the pom) * @param m2RepositoryPath the path to the m2repository */ - public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includeTest) { + public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE sourceType) { this.m2RepositoryPath = m2RepositoryPath; if (!new File(projectRoot).isDirectory()) { @@ -65,13 +70,15 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, boolean includ } // source - List sourceDirectories = model.getSourceDirectories(); - for (File sourceDirectory : sourceDirectories) { - this.addInputResource(sourceDirectory.getAbsolutePath()); + if (sourceType == SOURCE_TYPE.SOURCE || sourceType == SOURCE_TYPE.ALL) { + List sourceDirectories = model.getSourceDirectories(); + for (File sourceDirectory : sourceDirectories) { + this.addInputResource(sourceDirectory.getAbsolutePath()); + } } // test - if (includeTest) { + if (sourceType == SOURCE_TYPE.TEST || sourceType == SOURCE_TYPE.ALL) { List testSourceDirectories = model.getTestDirectories(); for (File sourceDirectory : testSourceDirectories) { this.addInputResource(sourceDirectory.getAbsolutePath()); From 58273cfe4686be5e1646dd907392357a9259859a Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 21:08:46 +0200 Subject: [PATCH 09/33] add generated source in the model --- src/main/java/spoon/MavenLauncher.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index e2f7dcf1240..b149fdd77a1 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -38,8 +38,11 @@ public class MavenLauncher extends Launcher { private String m2RepositoryPath; public enum SOURCE_TYPE { + // only the main code of the application SOURCE, + // only the tests of the application TEST, + // all the sources ALL } @@ -149,6 +152,10 @@ public List getSourceDirectories() { if (source.exists()) { output.add(source); } + File generatedSource = Paths.get(directory.getAbsolutePath(), "target", "generated-sources").toFile(); + if (generatedSource.exists()) { + output.add(generatedSource); + } for (InheritanceModel module : modules) { output.addAll(module.getSourceDirectories()); } @@ -170,6 +177,10 @@ public List getTestDirectories() { if (source.exists()) { output.add(source); } + File generatedSource = Paths.get(directory.getAbsolutePath(), "target", "generated-test-sources").toFile(); + if (generatedSource.exists()) { + output.add(generatedSource); + } for (InheritanceModel module : modules) { output.addAll(module.getTestDirectories()); } From 4e51132b81fca4628214d7000780e809e35b604d Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 15 Sep 2017 21:10:37 +0200 Subject: [PATCH 10/33] fix test --- src/test/java/spoon/MavenLauncherTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 62d4e426175..2dc5f6d9bd7 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -8,13 +8,13 @@ public class MavenLauncherTest { @Test public void spoonMavenLauncherTest() { // without the tests - MavenLauncher launcher = new MavenLauncher("./", false); + MavenLauncher launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.SOURCE); assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); // 54 because of the sub folder assertEquals(54, launcher.getModelBuilder().getInputSources().size()); // with the tests - launcher = new MavenLauncher("./", true); + launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL); assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); // 54 because of the sub folder assertEquals(235, launcher.getModelBuilder().getInputSources().size()); From de3add0a6d8d99e0648d70a2833c6d3ecf0db838 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 18 Sep 2017 10:01:27 +0200 Subject: [PATCH 11/33] add the parent in the dependency list --- src/main/java/spoon/MavenLauncher.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index b149fdd77a1..74e62091bce 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -19,6 +19,7 @@ import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; +import org.apache.maven.model.Parent; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -69,7 +70,7 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so throw new SpoonException("Unable to read the pom", e); } if (model == null) { - throw new SpoonException(" Unable to create the model, pom not found?"); + throw new SpoonException("Unable to create the model, pom not found?"); } // source @@ -190,6 +191,21 @@ public List getTestDirectories() { public List getDependencies() { Set output = new HashSet<>(); + // add the parent has a dependency + Parent parent = model.getParent(); + if (parent != null) { + String groupId = parent.getGroupId().replace(".", "/"); + String version = parent.getVersion(); + if (version.startsWith("$")) { + version = getProperty(version.substring(2, version.length() - 1)); + } + String fileName = parent.getArtifactId() + "-" + version + ".jar"; + Path depPath = Paths.get(m2RepositoryPath, groupId, parent.getArtifactId(), version, fileName); + File jar = depPath.toFile(); + if (jar.exists()) { + output.add(jar); + } + } List dependencies = model.getDependencies(); for (Dependency dependency : dependencies) { String groupId = dependency.getGroupId().replace(".", "/"); From 72afe67046854100792ea1f3405a656c7498efa4 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 18 Sep 2017 10:16:31 +0200 Subject: [PATCH 12/33] compute the compliance level --- src/main/java/spoon/MavenLauncher.java | 48 ++++++++++++++++++++-- src/test/java/spoon/MavenLauncherTest.java | 3 ++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 74e62091bce..20c1acba41e 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -20,7 +20,9 @@ import org.apache.maven.model.Dependency; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; +import org.apache.maven.model.Plugin; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; @@ -97,6 +99,9 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so classpath[i] = file.getAbsolutePath(); } this.getModelBuilder().setSourceClasspath(classpath); + + // compliance level + this.getEnvironment().setComplianceLevel(model.getJavaVersion()); } @@ -188,6 +193,12 @@ public List getTestDirectories() { return output; } + private String extractVariable(String value) { + if (value.startsWith("$")) { + value = getProperty(value.substring(2, value.length() - 1)); + } + return value; + } public List getDependencies() { Set output = new HashSet<>(); @@ -195,10 +206,7 @@ public List getDependencies() { Parent parent = model.getParent(); if (parent != null) { String groupId = parent.getGroupId().replace(".", "/"); - String version = parent.getVersion(); - if (version.startsWith("$")) { - version = getProperty(version.substring(2, version.length() - 1)); - } + String version = extractVariable(parent.getVersion()); String fileName = parent.getArtifactId() + "-" + version + ".jar"; Path depPath = Paths.get(m2RepositoryPath, groupId, parent.getArtifactId(), version, fileName); File jar = depPath.toFile(); @@ -246,6 +254,38 @@ private String getProperty(String key) { return value; } + public int getJavaVersion() { + for (Plugin plugin : model.getBuild().getPlugins()) { + if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) { + continue; + } + Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration(); + Xpp3Dom source = configuration.getChild("source"); + if (source != null) { + return Integer.parseInt(extractVariable(source.getValue()).substring(2)); + } + break; + } + String javaVersion = getProperty("java.version"); + if (javaVersion != null) { + return Integer.parseInt(extractVariable(javaVersion).substring(2)); + } + javaVersion = getProperty("java.src.version"); + if (javaVersion != null) { + return Integer.parseInt(extractVariable(javaVersion).substring(2)); + } + javaVersion = getProperty("maven.compiler.source"); + if (javaVersion != null) { + return Integer.parseInt(extractVariable(javaVersion).substring(2)); + } + javaVersion = getProperty("maven.compile.source"); + if (javaVersion != null) { + return Integer.parseInt(extractVariable(javaVersion).substring(2)); + } + // return the default java 7 version + return 7; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 2dc5f6d9bd7..4a0ae267004 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -9,6 +9,9 @@ public class MavenLauncherTest { public void spoonMavenLauncherTest() { // without the tests MavenLauncher launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.SOURCE); + + assertEquals(8, launcher.getEnvironment().getComplianceLevel()); + assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); // 54 because of the sub folder assertEquals(54, launcher.getModelBuilder().getInputSources().size()); From e1488c5c0989fc3f4188880dcc21fc7acb0c6298 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 18 Sep 2017 10:36:42 +0200 Subject: [PATCH 13/33] add some basic tests --- src/main/java/spoon/MavenLauncher.java | 7 ++----- src/test/java/spoon/MavenLauncherTest.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 20c1acba41e..7fbf0f519fd 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -62,7 +62,7 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so this.m2RepositoryPath = m2RepositoryPath; if (!new File(projectRoot).isDirectory()) { - throw new SpoonException(projectRoot + " has to be a folder"); + throw new SpoonException(projectRoot + " has to be the root folder of the project the folder that contains the pom)."); } InheritanceModel model; @@ -217,10 +217,7 @@ public List getDependencies() { List dependencies = model.getDependencies(); for (Dependency dependency : dependencies) { String groupId = dependency.getGroupId().replace(".", "/"); - String version = dependency.getVersion(); - if (version.startsWith("$")) { - version = getProperty(version.substring(2, version.length() - 1)); - } + String version = extractVariable(dependency.getVersion()); String fileName = dependency.getArtifactId() + "-" + version + ".jar"; Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version, fileName); File jar = depPath.toFile(); diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 4a0ae267004..082360e81a9 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -22,4 +22,14 @@ public void spoonMavenLauncherTest() { // 54 because of the sub folder assertEquals(235, launcher.getModelBuilder().getInputSources().size()); } + + @Test(expected = SpoonException.class) + public void MavenLauncherOnFileTest() { + new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.SOURCE); + } + + @Test(expected = SpoonException.class) + public void MavenLauncherOnDirectoryWithoutPolTest() { + new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.SOURCE); + } } From dfbfed01e92a466cba7e7c528b71262d05adcd05 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 18 Sep 2017 10:37:57 +0200 Subject: [PATCH 14/33] fix test name --- src/test/java/spoon/MavenLauncherTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 082360e81a9..82e6fb2ab7a 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -29,7 +29,7 @@ public void MavenLauncherOnFileTest() { } @Test(expected = SpoonException.class) - public void MavenLauncherOnDirectoryWithoutPolTest() { + public void MavenLauncherOnDirectoryWithoutPomTest() { new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.SOURCE); } } From 7318a84e1d1347bec0d4c11ca3e075bad4f639d4 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Mon, 18 Sep 2017 10:44:43 +0200 Subject: [PATCH 15/33] add javadoc --- src/main/java/spoon/MavenLauncher.java | 47 ++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 7fbf0f519fd..c6d504ec0f4 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -40,6 +40,10 @@ */ public class MavenLauncher extends Launcher { private String m2RepositoryPath; + + /** + * The type of source to consider in the model + */ public enum SOURCE_TYPE { // only the main code of the application SOURCE, @@ -101,10 +105,17 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so this.getModelBuilder().setSourceClasspath(classpath); // compliance level - this.getEnvironment().setComplianceLevel(model.getJavaVersion()); + this.getEnvironment().setComplianceLevel(model.getSourceVersion()); } - + /** + * Extract the information from the pom + * @param path the path to the pom + * @param parent the parent pom + * @return the extracted model + * @throws IOException when the file does not exist + * @throws XmlPullParserException when the file is corrupted + */ private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException { File pomFile = Paths.get(path, "pom.xml").toFile(); if (!pomFile.exists()) { @@ -139,10 +150,18 @@ public Model getModel() { return model; } + /** + * Get the parent model + * @return the parent model + */ public InheritanceModel getParent() { return parent; } + /** + * Get the list of source directories of the project + * @return the list of source directories + */ public List getSourceDirectories() { List output = new ArrayList<>(); String sourcePath = null; @@ -168,6 +187,10 @@ public List getSourceDirectories() { return output; } + /** + * Get the list of test directories of the project + * @return the list of test directories + */ public List getTestDirectories() { List output = new ArrayList<>(); String sourcePath = null; @@ -193,12 +216,20 @@ public List getTestDirectories() { return output; } + /** + * Extract the variable from a string + */ private String extractVariable(String value) { if (value.startsWith("$")) { value = getProperty(value.substring(2, value.length() - 1)); } return value; } + + /** + * Get the list of dependencies available in the local maven repository + * @return the list of dependencies + */ public List getDependencies() { Set output = new HashSet<>(); @@ -219,6 +250,7 @@ public List getDependencies() { String groupId = dependency.getGroupId().replace(".", "/"); String version = extractVariable(dependency.getVersion()); String fileName = dependency.getArtifactId() + "-" + version + ".jar"; + // TODO: Check the scope of the dependency (local dependency is not handled) Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version, fileName); File jar = depPath.toFile(); if (jar.exists()) { @@ -235,6 +267,11 @@ public List getDependencies() { return new ArrayList<>(output); } + /** + * Get the value of a property + * @param key the key of the property + * @return the property value if key exists or null + */ private String getProperty(String key) { if ("project.version".equals(key)) { if (model.getVersion() != null) { @@ -251,7 +288,11 @@ private String getProperty(String key) { return value; } - public int getJavaVersion() { + /** + * Get the source version of the project (default 7) + * @return the source version of the project + */ + public int getSourceVersion() { for (Plugin plugin : model.getBuild().getPlugins()) { if (!"maven-compiler-plugin".equals(plugin.getArtifactId())) { continue; From 1ccbd5af5d827ef5d6e13512fc134cde5f26a3a8 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 07:55:57 +0200 Subject: [PATCH 16/33] fix comment in test --- src/test/java/spoon/MavenLauncherTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 82e6fb2ab7a..b1a8b3141a6 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -13,13 +13,13 @@ public void spoonMavenLauncherTest() { assertEquals(8, launcher.getEnvironment().getComplianceLevel()); assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); - // 54 because of the sub folder + // 54 because of the sub folders of src/main/java assertEquals(54, launcher.getModelBuilder().getInputSources().size()); // with the tests launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL); assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); - // 54 because of the sub folder + // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); } From 825658481c6fc1933dc024efc6c4f24b8cd2d732 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 07:58:45 +0200 Subject: [PATCH 17/33] rename source type --- src/main/java/spoon/MavenLauncher.java | 14 +++++++------- src/test/java/spoon/MavenLauncherTest.java | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index c6d504ec0f4..64cca8aef02 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -46,11 +46,11 @@ public class MavenLauncher extends Launcher { */ public enum SOURCE_TYPE { // only the main code of the application - SOURCE, + APP_SOURCE, // only the tests of the application - TEST, + TEST_SOURCE, // all the sources - ALL + ALL_SOURCE } public MavenLauncher(String pomPatch, SOURCE_TYPE sourceType) { @@ -79,16 +79,16 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so throw new SpoonException("Unable to create the model, pom not found?"); } - // source - if (sourceType == SOURCE_TYPE.SOURCE || sourceType == SOURCE_TYPE.ALL) { + // app source + if (SOURCE_TYPE.APP_SOURCE == sourceType || SOURCE_TYPE.ALL_SOURCE == sourceType) { List sourceDirectories = model.getSourceDirectories(); for (File sourceDirectory : sourceDirectories) { this.addInputResource(sourceDirectory.getAbsolutePath()); } } - // test - if (sourceType == SOURCE_TYPE.TEST || sourceType == SOURCE_TYPE.ALL) { + // test source + if (SOURCE_TYPE.TEST_SOURCE == sourceType || SOURCE_TYPE.ALL_SOURCE == sourceType) { List testSourceDirectories = model.getTestDirectories(); for (File sourceDirectory : testSourceDirectories) { this.addInputResource(sourceDirectory.getAbsolutePath()); diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index b1a8b3141a6..405814dfd88 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -8,7 +8,7 @@ public class MavenLauncherTest { @Test public void spoonMavenLauncherTest() { // without the tests - MavenLauncher launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.SOURCE); + MavenLauncher launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.APP_SOURCE); assertEquals(8, launcher.getEnvironment().getComplianceLevel()); @@ -17,7 +17,7 @@ public void spoonMavenLauncherTest() { assertEquals(54, launcher.getModelBuilder().getInputSources().size()); // with the tests - launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL); + launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); @@ -25,11 +25,11 @@ public void spoonMavenLauncherTest() { @Test(expected = SpoonException.class) public void MavenLauncherOnFileTest() { - new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.SOURCE); + new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE); } @Test(expected = SpoonException.class) public void MavenLauncherOnDirectoryWithoutPomTest() { - new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.SOURCE); + new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.APP_SOURCE); } } From 320ae2c6112e94e1660f119119b49de54734055a Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 08:44:37 +0200 Subject: [PATCH 18/33] add doc --- doc/_data/sidebar_doc.yml | 7 ++++ doc/launcher.md | 69 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 doc/launcher.md diff --git a/doc/_data/sidebar_doc.yml b/doc/_data/sidebar_doc.yml index 2c93dadcda3..4891591684f 100755 --- a/doc/_data/sidebar_doc.yml +++ b/doc/_data/sidebar_doc.yml @@ -68,6 +68,13 @@ entries: version: all items: + - title: From Java + url: /launcher.html + audience: writers, designers + platform: all + product: all + version: all + - title: Command Line url: /command_line.html audience: writers, designers diff --git a/doc/launcher.md b/doc/launcher.md new file mode 100644 index 00000000000..26512c5ef6d --- /dev/null +++ b/doc/launcher.md @@ -0,0 +1,69 @@ +--- +title: Launcher +tags: [usage] +keywords: usage, java +--- + +## Dependency + +Stable version: + +```xml + + fr.inria.gforge.spoon + spoon-core + {{site.spoon_release}} + +``` + +Snapshot version: + +```xml + + + fr.inria.gforge.spoon + spoon-core + {{site.spoon_snapshot}} + + + + + gforge.inria.fr-snapshot + Maven Repository for Spoon Snapshot + http://spoon.gforge.inria.fr/repositories/snapshots/ + + + +``` + +## Launcher + +The Spoon `Launcher` ([JavaDoc](/mvnsites/spoon-core/apidocs/spoon/Launcher.html)) is used to create the AST model of the project. + +### Usage + +```Java +Launcher launcher = new Launcher(); +launcher.addInputResource(""); +launcher.getEnvironment().setAutoImports(true); // optional +launcher.getEnvironment().setComplianceLevel(7); // optional +launcher.getEnvironment().setNoClasspath(true); // optional +launcher.getEnvironment().setSourceClasspath(""); // optional +launcher.buildModel(); +CtModel model = launcher.getModel(); +``` + + +## MavenLauncher + +The Spoon `MavenLauncher` ([JavaDoc](/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html)) is used to create the AST model of the project by inferring automatically the list of source folder and the dependencies from a Maven `pom.xml` file. +This Launcher simplify the creation of model on complex multi-module project. +If you consider to only use `Processors` in your Spoon, consider to use the [Spoon Maven Plugin](/maven.html) which will do a better job to detect the dependencies. + +### Usage + +```Java +MavenLauncher launcher = new MavenLauncher("", MavenLauncher.SOURCE_TYPE.APP_SOURCE); +launcher.buildModel(); +CtModel model = launcher.getModel(); +``` \ No newline at end of file From f3a15395c3c2fdc11b4bbd1783fd585a449d5f82 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 09:52:04 +0200 Subject: [PATCH 19/33] fix doc --- doc/launcher.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/launcher.md b/doc/launcher.md index 26512c5ef6d..65ecb617c2d 100644 --- a/doc/launcher.md +++ b/doc/launcher.md @@ -38,11 +38,11 @@ Snapshot version: ## Launcher -The Spoon `Launcher` ([JavaDoc](/mvnsites/spoon-core/apidocs/spoon/Launcher.html)) is used to create the AST model of the project. +The Spoon `Launcher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/Launcher.html)) is used to create the AST model of the project. ### Usage -```Java +```java Launcher launcher = new Launcher(); launcher.addInputResource(""); launcher.getEnvironment().setAutoImports(true); // optional @@ -56,13 +56,13 @@ CtModel model = launcher.getModel(); ## MavenLauncher -The Spoon `MavenLauncher` ([JavaDoc](/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html)) is used to create the AST model of the project by inferring automatically the list of source folder and the dependencies from a Maven `pom.xml` file. +The Spoon `MavenLauncher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html)) is used to create the AST model of the project by inferring automatically the list of source folder and the dependencies from a Maven `pom.xml` file. This Launcher simplify the creation of model on complex multi-module project. -If you consider to only use `Processors` in your Spoon, consider to use the [Spoon Maven Plugin](/maven.html) which will do a better job to detect the dependencies. +If you consider to only use `Processors` in your Spoon, consider to use the [Spoon Maven Plugin](http://spoon.gforge.inria.fr/maven.html) which will do a better job to detect the dependencies. ### Usage -```Java +```java MavenLauncher launcher = new MavenLauncher("", MavenLauncher.SOURCE_TYPE.APP_SOURCE); launcher.buildModel(); CtModel model = launcher.getModel(); From b0badf885b3cf17b25674a906c72e52b1dbdb51f Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 10:19:04 +0200 Subject: [PATCH 20/33] rename constructor parameters --- src/main/java/spoon/MavenLauncher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 64cca8aef02..ddf7a890004 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -53,8 +53,8 @@ public enum SOURCE_TYPE { ALL_SOURCE } - public MavenLauncher(String pomPatch, SOURCE_TYPE sourceType) { - this(pomPatch, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), sourceType); + public MavenLauncher(String projectRoot, SOURCE_TYPE sourceType) { + this(projectRoot, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), sourceType); } /** From b8763ca6bf54c6ea2be3c2483f5ddc74fa27547e Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 13:40:42 +0200 Subject: [PATCH 21/33] add multi modules test --- src/test/java/spoon/MavenLauncherTest.java | 18 +- .../maven-launcher/pac4j/pac4j-cas/pom.xml | 79 +++ .../maven-launcher/pac4j/pac4j-config/pom.xml | 129 +++++ .../maven-launcher/pac4j/pac4j-core/pom.xml | 122 +++++ .../maven-launcher/pac4j/pac4j-couch/pom.xml | 81 +++ .../maven-launcher/pac4j/pac4j-gae/pom.xml | 99 ++++ .../maven-launcher/pac4j/pac4j-http/pom.xml | 123 +++++ .../maven-launcher/pac4j/pac4j-jwt/pom.xml | 74 +++ .../pac4j/pac4j-kerberos/pom.xml | 109 ++++ .../maven-launcher/pac4j/pac4j-ldap/pom.xml | 108 ++++ .../maven-launcher/pac4j/pac4j-mongo/pom.xml | 115 ++++ .../maven-launcher/pac4j/pac4j-oauth/pom.xml | 78 +++ .../maven-launcher/pac4j/pac4j-oidc/pom.xml | 75 +++ .../maven-launcher/pac4j/pac4j-openid/pom.xml | 86 +++ .../maven-launcher/pac4j/pac4j-saml/pom.xml | 222 ++++++++ .../maven-launcher/pac4j/pac4j-sql/pom.xml | 109 ++++ .../resources/maven-launcher/pac4j/pom.xml | 507 ++++++++++++++++++ 17 files changed, 2131 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-cas/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-config/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-core/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-couch/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-gae/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-http/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-jwt/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-kerberos/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-ldap/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-mongo/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-oauth/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-oidc/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-openid/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-saml/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pac4j-sql/pom.xml create mode 100644 src/test/resources/maven-launcher/pac4j/pom.xml diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 405814dfd88..e8e2d8b0ff9 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -21,15 +21,27 @@ public void spoonMavenLauncherTest() { assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); + + // specify the pom.xml + launcher = new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE); + assertEquals(8, launcher.getEnvironment().getComplianceLevel()); + } + + @Test + public void multiModulesProjectTest() { + MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/pac4j", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); + assertEquals(8, launcher.getEnvironment().getComplianceLevel()); + assertEquals(112, launcher.getEnvironment().getSourceClasspath().length); + assertEquals(0, launcher.getModelBuilder().getInputSources().size()); } @Test(expected = SpoonException.class) - public void MavenLauncherOnFileTest() { - new MavenLauncher("./pom.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE); + public void mavenLauncherOnANotExistingFileTest() { + new MavenLauncher("./pomm.xml", MavenLauncher.SOURCE_TYPE.APP_SOURCE); } @Test(expected = SpoonException.class) - public void MavenLauncherOnDirectoryWithoutPomTest() { + public void mavenLauncherOnDirectoryWithoutPomTest() { new MavenLauncher("./src", MavenLauncher.SOURCE_TYPE.APP_SOURCE); } } diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-cas/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-cas/pom.xml new file mode 100644 index 00000000000..171b8e5e51f --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-cas/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-cas + jar + pac4j for CAS protocol + + + 3.4.1 + + + + + org.pac4j + pac4j-core + + + org.jasig.cas.client + cas-client-core + ${cas.version} + + + org.jasig.cas.client + cas-client-support-saml + ${cas.version} + + + javax.servlet + javax.servlet-api + provided + + + com.google.guava + guava + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.cas + org.pac4j.cas.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-config/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-config/pom.xml new file mode 100644 index 00000000000..31c0ab0af3b --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-config/pom.xml @@ -0,0 +1,129 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-config + jar + pac4j configuration + + + + org.pac4j + pac4j-core + + + org.pac4j + pac4j-cas + true + + + org.pac4j + pac4j-saml + true + + + org.pac4j + pac4j-oauth + true + + + org.pac4j + pac4j-oidc + true + + + org.pac4j + pac4j-ldap + true + + + org.pac4j + pac4j-http + true + + + com.zaxxer + HikariCP + 2.6.1 + + + org.pac4j + pac4j-sql + true + + + org.springframework.security + spring-security-crypto + true + + + org.apache.shiro + shiro-core + true + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.pac4j + pac4j-ldap + test-jar + test + + + com.unboundid + unboundid-ldapsdk + test + + + org.pac4j + pac4j-sql + test-jar + test + + + com.h2database + h2 + ${h2.version} + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.config + org.pac4j.config.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-core/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-core/pom.xml new file mode 100644 index 00000000000..100ecb87171 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-core/pom.xml @@ -0,0 +1,122 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-core + jar + pac4j core + + + org.slf4j + slf4j-api + + + javax.servlet + javax.servlet-api + provided + + + com.google.guava + guava + true + + + org.springframework.security + spring-security-crypto + true + + + org.apache.shiro + shiro-core + true + + + + junit + junit + test + + + org.springframework + spring-test + test + + + org.slf4j + jcl-over-slf4j + test + + + ch.qos.logback + logback-classic + test + + + org.mockito + mockito-core + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.core + org.pac4j.core.*;version=${project.version} + * + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + jar + test-jar + + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-couch/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-couch/pom.xml new file mode 100644 index 00000000000..a92ba47e29e --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-couch/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-couch + jar + pac4j for CouchDB + + + 1.4.4 + + + + + org.pac4j + pac4j-core + + + org.ektorp + org.ektorp + ${ektorp.version} + + + com.fasterxml.jackson.core + jackson-databind + 2.3.3 + + + + org.pac4j + pac4j-core + test-jar + test + + + io.bdrc + mcouch-ektorp + 1.0.0 + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.apache.shiro + shiro-core + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.couch + org.pac4j.couch.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-gae/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-gae/pom.xml new file mode 100644 index 00000000000..ad0ba660e55 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-gae/pom.xml @@ -0,0 +1,99 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-gae + jar + pac4j for GAE UserService mechanism + + + 1.9.50 + 1.1 + + + + + org.pac4j + pac4j-core + + + com.google.appengine + appengine-api-1.0-sdk + ${gae.version} + + + com.google.appengine + appengine-jsr107cache + ${gae.version} + + + net.sf.jsr107cache + jsr107cache + ${jsr107cache.version} + + + javax.servlet + javax.servlet-api + provided + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + com.google.appengine + appengine-testing + ${gae.version} + test + + + com.google.appengine + appengine-api-labs + ${gae.version} + test + + + com.google.appengine + appengine-api-stubs + ${gae.version} + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.gae + org.pac4j.gae.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-http/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-http/pom.xml new file mode 100644 index 00000000000..c7b9fc54cef --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-http/pom.xml @@ -0,0 +1,123 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-http + jar + pac4j for HTTP protocol + + + 2.3.1 + + + + + org.pac4j + pac4j-core + + + commons-codec + commons-codec + true + + + com.fasterxml.jackson.core + jackson-databind + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.mockito + mockito-core + test + + + com.google.guava + guava + test + + + org.nanohttpd + nanohttpd + ${nanohttpd.version} + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.http + org.pac4j.http.*;version=${project.version} + * + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + jar + test-jar + + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-jwt/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-jwt/pom.xml new file mode 100644 index 00000000000..286f4484d2a --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-jwt/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-jwt + jar + pac4j for JWT + + + 1.56 + + + + + org.pac4j + pac4j-core + + + com.nimbusds + nimbus-jose-jwt + + + org.bouncycastle + bcprov-jdk15on + ${bcprov.version} + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.pac4j + pac4j-oauth + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.jwt + org.pac4j.jwt.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-kerberos/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-kerberos/pom.xml new file mode 100644 index 00000000000..a9249bc8ae5 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-kerberos/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-kerberos + jar + pac4j for Kerberos + + + 1.0.0 + + + + + org.pac4j + pac4j-core + + + + org.springframework + spring-core + ${spring.version} + + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.mockito + mockito-core + test + + + javax.servlet + javax.servlet-api + provided + + + + org.apache.kerby + kerby-kdc + ${kerby.version} + test + + + org.apache.kerby + kerb-simplekdc + ${kerby.version} + test + + + org.apache.kerby + kerb-client + ${kerby.version} + test + + + org.apache.kerby + token-provider + ${kerby.version} + test + + + org.apache.kerby + integration-test + ${kerby.version} + test + + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.kerberos + org.pac4j.kerberos.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-ldap/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-ldap/pom.xml new file mode 100644 index 00000000000..48519f201eb --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-ldap/pom.xml @@ -0,0 +1,108 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-ldap + jar + pac4j for LDAP + + + 1.2.1 + + + + + org.pac4j + pac4j-core + + + org.ldaptive + ldaptive + ${ldaptive.version} + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + com.unboundid + unboundid-ldapsdk + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.ldap + org.pac4j.ldap.*;version=${project.version} + * + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + jar + test-jar + + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-mongo/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-mongo/pom.xml new file mode 100644 index 00000000000..4d0beae60b4 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-mongo/pom.xml @@ -0,0 +1,115 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-mongo + jar + pac4j for MongoDB + + + 3.4.2 + 2.0.0 + + + + + org.pac4j + pac4j-core + + + org.mongodb + mongo-java-driver + ${mongo-driver.version} + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.apache.shiro + shiro-core + test + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + ${flapdoodle.version} + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.mongo + org.pac4j.mongo.*;version=${project.version} + * + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + jar + test-jar + + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-oauth/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-oauth/pom.xml new file mode 100644 index 00000000000..41b39e42a13 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-oauth/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-oauth + jar + pac4j for OAuth protocol + + + 3.3.0 + + + + + org.pac4j + pac4j-core + + + commons-codec + commons-codec + + + com.github.scribejava + scribejava-apis + ${scribe.version} + + + com.fasterxml.jackson.core + jackson-databind + + + javax.servlet + javax.servlet-api + provided + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.oauth + org.pac4j.oauth.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-oidc/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-oidc/pom.xml new file mode 100644 index 00000000000..bde81cf376d --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-oidc/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-oidc + jar + pac4j for OpenID Connect protocol + + + 5.24.2 + + + + + org.pac4j + pac4j-core + + + com.nimbusds + oauth2-oidc-sdk + ${oauth-oidc-sdk.version} + + + com.nimbusds + nimbus-jose-jwt + + + + + com.nimbusds + nimbus-jose-jwt + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.oidc + org.pac4j.oidc.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-openid/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-openid/pom.xml new file mode 100644 index 00000000000..f25f9de3afd --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-openid/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-openid + jar + pac4j for OpenId protocol + + + 1.0.0 + 2.0.2 + + + + + org.pac4j + pac4j-core + + + org.openid4java + openid4java + ${openid4java.version} + + + commons-logging + commons-logging + + + + + org.slf4j + jcl-over-slf4j + + + xml-apis + xml-apis + ${xml-apis.version} + + + javax.servlet + javax.servlet-api + provided + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.openid + org.pac4j.openid.*;version=${project.version} + * + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-saml/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-saml/pom.xml new file mode 100644 index 00000000000..8e0102a3687 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-saml/pom.xml @@ -0,0 +1,222 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-saml + jar + pac4j for SAML protocol + + + 3.3.0 + 2.9.2 + 1.7 + + 3.2.2 + 1.1.0 + 2.7.2 + 7.3.0 + + + + + org.pac4j + pac4j-core + + + org.opensaml + opensaml-core + ${opensaml.version} + compile + + + net.shibboleth.utilities + java-support + ${java-support.version} + compile + + + org.opensaml + opensaml-saml-api + ${opensaml.version} + compile + + + org.opensaml + opensaml-saml-impl + ${opensaml.version} + compile + + + org.opensaml + opensaml-soap-api + ${opensaml.version} + compile + + + org.opensaml + opensaml-xmlsec-api + ${opensaml.version} + + + org.opensaml + opensaml-security-api + ${opensaml.version} + + + org.opensaml + opensaml-security-impl + ${opensaml.version} + + + org.opensaml + opensaml-profile-api + ${opensaml.version} + + + org.opensaml + opensaml-profile-impl + ${opensaml.version} + + + org.opensaml + opensaml-messaging-api + ${opensaml.version} + + + org.opensaml + opensaml-messaging-impl + ${opensaml.version} + + + org.opensaml + opensaml-xmlsec-impl + ${opensaml.version} + + + com.google.guava + guava + + + org.cryptacular + cryptacular + ${cryptacular.version} + + + bcprov-jdk15on + org.bouncycastle + + + + + joda-time + joda-time + ${joda-time.version} + + + xalan + xalan + ${xalan.version} + + + org.apache.velocity + velocity + ${velocity.version} + + + commons-collections + commons-collections + ${commons-collections.version} + + + org.slf4j + jcl-over-slf4j + + + javax.servlet + javax.servlet-api + provided + + + org.springframework + spring-core + compile + ${spring.version} + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + ch.qos.logback + logback-classic + test + + + org.springframework + spring-test + test + + + org.springframework + spring-web + ${spring.version} + test + + + commons-logging + commons-logging + + + + + org.mockito + mockito-core + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.saml2 + org.pac4j.saml.*;version=${project.version} + org.joda.time;version="[1.6,3)",* + + + + + + + + + shib-release + https://build.shibboleth.net/nexus/content/groups/public + + false + + + true + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pac4j-sql/pom.xml b/src/test/resources/maven-launcher/pac4j/pac4j-sql/pom.xml new file mode 100644 index 00000000000..780e552a935 --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pac4j-sql/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + + + org.pac4j + pac4j + 3.0.0-SNAPSHOT + + + pac4j-sql + jar + pac4j for relational DB + + + 2.78 + + + + + org.pac4j + pac4j-core + + + org.jdbi + jdbi + ${jdbi.version} + + + + org.pac4j + pac4j-core + test-jar + test + + + junit + junit + test + + + org.springframework.security + spring-security-crypto + test + + + com.h2database + h2 + ${h2.version} + test + + + + + + + + org.apache.felix + maven-bundle-plugin + + + org.pac4j.sql + org.pac4j.sql.*;version=${project.version} + * + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + test-jar + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + jar + test-jar + + + + + + + + diff --git a/src/test/resources/maven-launcher/pac4j/pom.xml b/src/test/resources/maven-launcher/pac4j/pom.xml new file mode 100644 index 00000000000..2425b9e7f2a --- /dev/null +++ b/src/test/resources/maven-launcher/pac4j/pom.xml @@ -0,0 +1,507 @@ + + + 4.0.0 + + + org.sonatype.oss + oss-parent + 9 + + + org.pac4j + pac4j + pom + pac4j + 3.0.0-SNAPSHOT + Profile & Authentication Client for Java + https://github.com/pac4j/pac4j + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + https://github.com/pac4j/pac4j.git + scm:git:git@github.com:pac4j/pac4j.git + scm:git:git@github.com:pac4j/pac4j.git + + + + + leleuj + Jerome LELEU + leleuj@gmail.com + + + + + pac4j-core + pac4j-config + pac4j-oauth + pac4j-cas + pac4j-openid + pac4j-http + pac4j-saml + pac4j-gae + pac4j-oidc + pac4j-jwt + pac4j-ldap + pac4j-sql + pac4j-mongo + pac4j-couch + pac4j-kerberos + + + + 4.12 + 3.1.0 + 1.2.2 + 1.10 + 21.0 + 4.35 + 4.3.7.RELEASE + 4.2.2.RELEASE + 1.3.2 + 2.7.19 + 1.7.25 + 3.2.1 + 1.4.194 + 1.8 + 2.8.7 + UTF-8 + UTF-8 + + + + + + org.pac4j + pac4j-core + ${project.version} + + + org.pac4j + pac4j-oauth + ${project.version} + + + org.pac4j + pac4j-cas + ${project.version} + + + org.pac4j + pac4j-saml + ${project.version} + + + org.pac4j + pac4j-oidc + ${project.version} + + + org.pac4j + pac4j-ldap + ${project.version} + + + org.pac4j + pac4j-http + ${project.version} + + + org.pac4j + pac4j-sql + ${project.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.springframework.security + spring-security-crypto + ${spring.security.version} + + + org.apache.shiro + shiro-core + ${shiro.version} + + + commons-codec + commons-codec + ${commons-codec.version} + + + javax.servlet + javax.servlet-api + ${servlet-api.version} + + + com.google.guava + guava + ${guava.version} + + + com.nimbusds + nimbus-jose-jwt + ${nimbus-jose-jwt.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + org.pac4j + pac4j-core + ${project.version} + test-jar + + + org.pac4j + pac4j-ldap + ${project.version} + test-jar + + + org.pac4j + pac4j-sql + ${project.version} + test-jar + + + junit + junit + ${junit.version} + + + org.springframework + spring-test + ${spring.version} + + + commons-logging + commons-logging + + + + + org.slf4j + jcl-over-slf4j + ${slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.mockito + mockito-core + ${mockito.version} + + + com.unboundid + unboundid-ldapsdk + ${unboundid.version} + + + com.h2database + h2 + ${h2.version} + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + + package + + shade + + + + ${project.artifactId}-${project.version}-all.jar + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/*Tests.java + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.17 + + checkstyle.xml + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.6 + + + + integration-test + verify + + + + + true + + **/*IT.java + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5 + + ${java.version} + ${java.version} + UTF-8 + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + regex-property + + regex-property + + + module.suffix + ${project.artifactId} + ^(pac4j\-)(.*)$ + $2 + false + + + + set-osgi-version + verify + + parse-version + + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + verify + + jar + + + + + + + 2 + ${project.name} + ${project.groupId}.${module.suffix}.source + ${organization.name} + ${parsedVersion.osgiVersion} + ${project.groupId}.${module.suffix};version="${parsedVersion.osgiVersion}";roots:="." + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.3 + + Low + Max + true + ${basedir}/../findbugs-exclude.xml + true + + + + run-findbugs + compile + + check + + + + + + org.apache.maven.plugins + maven-pmd-plugin + 3.6 + + true + true + + + + run-pmd + compile + + check + + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + + + org.apache.maven.plugins + maven-source-plugin + 2.4 + + + org.apache.felix + maven-bundle-plugin + 3.0.1 + true + + + bundle-manifest + process-classes + + manifest + + + + + + + jar + + + + + + + + + + release-sign-artifacts + + + performRelease + true + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + + + + forceIT + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.6 + + + + integration-test + verify + + + + + false + + **/*IT.java + + + + + + + + + From d8ba4c85e8d4e62682fccf6e89d81e6692d0f7a5 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 13:42:13 +0200 Subject: [PATCH 22/33] get the dependencies of the dependencies --- src/main/java/spoon/MavenLauncher.java | 74 +++++++++++++++++++------- 1 file changed, 56 insertions(+), 18 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index ddf7a890004..d6b57112789 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -40,6 +40,7 @@ */ public class MavenLauncher extends Launcher { private String m2RepositoryPath; + private SOURCE_TYPE sourceType; /** * The type of source to consider in the model @@ -53,25 +54,30 @@ public enum SOURCE_TYPE { ALL_SOURCE } - public MavenLauncher(String projectRoot, SOURCE_TYPE sourceType) { - this(projectRoot, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), sourceType); + public MavenLauncher(String mavenProject, SOURCE_TYPE sourceType) { + this(mavenProject, Paths.get(System.getProperty("user.home"), ".m2", "repository").toString(), sourceType); } /** * - * @param projectRoot the path to the root of the project (the folder that contains the pom) + * @param mavenProject the path to the root of the project * @param m2RepositoryPath the path to the m2repository */ - public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE sourceType) { + public MavenLauncher(String mavenProject, String m2RepositoryPath, SOURCE_TYPE sourceType) { this.m2RepositoryPath = m2RepositoryPath; + this.sourceType = sourceType; - if (!new File(projectRoot).isDirectory()) { - throw new SpoonException(projectRoot + " has to be the root folder of the project the folder that contains the pom)."); + File mavenProjectFile = new File(mavenProject); + if (!mavenProjectFile.exists()) { + throw new SpoonException(mavenProject + " does not exist."); + } + if (mavenProjectFile.isFile()) { + mavenProject = mavenProject.substring(0, mavenProject.length() - mavenProjectFile.getName().length() - 1); } InheritanceModel model; try { - model = readPOM(projectRoot, null); + model = readPOM(mavenProject, null); } catch (Exception e) { throw new SpoonException("Unable to read the pom", e); } @@ -117,17 +123,22 @@ public MavenLauncher(String projectRoot, String m2RepositoryPath, SOURCE_TYPE so * @throws XmlPullParserException when the file is corrupted */ private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException { - File pomFile = Paths.get(path, "pom.xml").toFile(); + if (!path.endsWith(".xml") && !path.endsWith(".pom")) { + path = Paths.get(path, "pom.xml").toString(); + } + File pomFile = new File(path); if (!pomFile.exists()) { return null; } MavenXpp3Reader pomReader = new MavenXpp3Reader(); - Model model = pomReader.read(new FileReader(pomFile)); - InheritanceModel inheritanceModel = new InheritanceModel(model, parent, new File(path)); - for (String module : model.getModules()) { - inheritanceModel.addModule(readPOM(Paths.get(path, module).toString(), inheritanceModel)); + try (FileReader reader = new FileReader(pomFile)) { + Model model = pomReader.read(reader); + InheritanceModel inheritanceModel = new InheritanceModel(model, parent, pomFile.getParentFile()); + for (String module : model.getModules()) { + inheritanceModel.addModule(readPOM(Paths.get(pomFile.getParent(), module).toString(), inheritanceModel)); + } + return inheritanceModel; } - return inheritanceModel; } class InheritanceModel { @@ -238,6 +249,9 @@ public List getDependencies() { if (parent != null) { String groupId = parent.getGroupId().replace(".", "/"); String version = extractVariable(parent.getVersion()); + if (version.startsWith("[")) { + version = version.substring(1, version.indexOf(',')); + } String fileName = parent.getArtifactId() + "-" + version + ".jar"; Path depPath = Paths.get(m2RepositoryPath, groupId, parent.getArtifactId(), version, fileName); File jar = depPath.toFile(); @@ -248,13 +262,37 @@ public List getDependencies() { List dependencies = model.getDependencies(); for (Dependency dependency : dependencies) { String groupId = dependency.getGroupId().replace(".", "/"); + if (dependency.getVersion() == null) { + continue; + } + // TODO: Handle range version String version = extractVariable(dependency.getVersion()); - String fileName = dependency.getArtifactId() + "-" + version + ".jar"; + if (version.startsWith("[")) { + version = version.substring(1, version.indexOf(',')); + } + if (dependency.isOptional()) { + continue; + } + // ignore test dependencies for app source code + if ("test".equals(dependency.getScope()) && SOURCE_TYPE.APP_SOURCE == sourceType) { + continue; + } + String fileName = dependency.getArtifactId() + "-" + version ; // TODO: Check the scope of the dependency (local dependency is not handled) - Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version, fileName); - File jar = depPath.toFile(); - if (jar.exists()) { - output.add(jar); + Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version); + File depFile = depPath.toFile(); + if (depFile.exists()) { + File jarFile = Paths.get(depPath.toString(), fileName + ".jar").toFile(); + if (jarFile.exists()) { + output.add(jarFile); + } + + try { + InheritanceModel dependencyModel = readPOM(Paths.get(depPath.toString(), fileName + ".pom").toString(), null); + output.addAll(dependencyModel.getDependencies()); + } catch (Exception ignore) { + // ignore the dependencies of the dependency + } } else { // if the a dependency is not found, uses the no classpath mode getEnvironment().setNoClasspath(true); From 8d7a44d266b8463a14d06aea10aca071097871ea Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 13:43:07 +0200 Subject: [PATCH 23/33] fix test --- src/test/java/spoon/MavenLauncherTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index e8e2d8b0ff9..5a402deac82 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -12,13 +12,13 @@ public void spoonMavenLauncherTest() { assertEquals(8, launcher.getEnvironment().getComplianceLevel()); - assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); + assertEquals(6, launcher.getEnvironment().getSourceClasspath().length); // 54 because of the sub folders of src/main/java assertEquals(54, launcher.getModelBuilder().getInputSources().size()); // with the tests launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); - assertEquals(10, launcher.getEnvironment().getSourceClasspath().length); + assertEquals(21, launcher.getEnvironment().getSourceClasspath().length); // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); From 320f2d5964a6cfe9d3882375cd590ef0d753470e Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 13:54:42 +0200 Subject: [PATCH 24/33] fix checkstyle --- src/main/java/spoon/MavenLauncher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index d6b57112789..01220617274 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -277,7 +277,7 @@ public List getDependencies() { if ("test".equals(dependency.getScope()) && SOURCE_TYPE.APP_SOURCE == sourceType) { continue; } - String fileName = dependency.getArtifactId() + "-" + version ; + String fileName = dependency.getArtifactId() + "-" + version; // TODO: Check the scope of the dependency (local dependency is not handled) Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version); File depFile = depPath.toFile(); From db143da2d4d7d3f8c0a38da1ba4b5f4ad0dac70a Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 14:13:16 +0200 Subject: [PATCH 25/33] improve transitive dependencies --- src/main/java/spoon/MavenLauncher.java | 15 +++++++++++---- src/test/java/spoon/MavenLauncherTest.java | 3 +-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 01220617274..55395e2b596 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -102,7 +102,7 @@ public MavenLauncher(String mavenProject, String m2RepositoryPath, SOURCE_TYPE s } // dependencies - List dependencies = model.getDependencies(); + List dependencies = model.getDependencies(false); String[] classpath = new String[dependencies.size()]; for (int i = 0; i < dependencies.size(); i++) { File file = dependencies.get(i); @@ -241,7 +241,7 @@ private String extractVariable(String value) { * Get the list of dependencies available in the local maven repository * @return the list of dependencies */ - public List getDependencies() { + public List getDependencies(boolean isLib) { Set output = new HashSet<>(); // add the parent has a dependency @@ -277,6 +277,10 @@ public List getDependencies() { if ("test".equals(dependency.getScope()) && SOURCE_TYPE.APP_SOURCE == sourceType) { continue; } + // ignore not transitive dependencies + if (isLib && ("test".equals(dependency.getScope()) || "provided".equals(dependency.getScope()))) { + continue; + } String fileName = dependency.getArtifactId() + "-" + version; // TODO: Check the scope of the dependency (local dependency is not handled) Path depPath = Paths.get(m2RepositoryPath, groupId, dependency.getArtifactId(), version); @@ -285,11 +289,14 @@ public List getDependencies() { File jarFile = Paths.get(depPath.toString(), fileName + ".jar").toFile(); if (jarFile.exists()) { output.add(jarFile); + } else { + // if the a dependency is not found, uses the no classpath mode + getEnvironment().setNoClasspath(true); } try { InheritanceModel dependencyModel = readPOM(Paths.get(depPath.toString(), fileName + ".pom").toString(), null); - output.addAll(dependencyModel.getDependencies()); + output.addAll(dependencyModel.getDependencies(true)); } catch (Exception ignore) { // ignore the dependencies of the dependency } @@ -300,7 +307,7 @@ public List getDependencies() { } for (InheritanceModel module : modules) { - output.addAll(module.getDependencies()); + output.addAll(module.getDependencies(isLib)); } return new ArrayList<>(output); } diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 5a402deac82..5046b66d18e 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -18,7 +18,7 @@ public void spoonMavenLauncherTest() { // with the tests launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); - assertEquals(21, launcher.getEnvironment().getSourceClasspath().length); + assertEquals(14, launcher.getEnvironment().getSourceClasspath().length); // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); @@ -31,7 +31,6 @@ public void spoonMavenLauncherTest() { public void multiModulesProjectTest() { MavenLauncher launcher = new MavenLauncher("./src/test/resources/maven-launcher/pac4j", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); assertEquals(8, launcher.getEnvironment().getComplianceLevel()); - assertEquals(112, launcher.getEnvironment().getSourceClasspath().length); assertEquals(0, launcher.getModelBuilder().getInputSources().size()); } From 31e87af07ef314a45ebaf0a3fe0e440dc6db2f1f Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 14:15:14 +0200 Subject: [PATCH 26/33] change the default compliance level --- src/main/java/spoon/MavenLauncher.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 55395e2b596..f86b1920031 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -365,8 +365,8 @@ public int getSourceVersion() { if (javaVersion != null) { return Integer.parseInt(extractVariable(javaVersion).substring(2)); } - // return the default java 7 version - return 7; + // return the current compliance level of spoon + return getEnvironment().getComplianceLevel(); } @Override From 2ad0ffc719e648e4344cfce1e1b0c98391d862a3 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 14:15:53 +0200 Subject: [PATCH 27/33] call parent constructor --- src/main/java/spoon/MavenLauncher.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index f86b1920031..f0b73075cd2 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -64,6 +64,7 @@ public MavenLauncher(String mavenProject, SOURCE_TYPE sourceType) { * @param m2RepositoryPath the path to the m2repository */ public MavenLauncher(String mavenProject, String m2RepositoryPath, SOURCE_TYPE sourceType) { + super(); this.m2RepositoryPath = m2RepositoryPath; this.sourceType = sourceType; From a9f403453c6da79849d67e2e3e9f9f38e1d34ebd Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 14:32:06 +0200 Subject: [PATCH 28/33] fix test --- src/test/java/spoon/MavenLauncherTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/spoon/MavenLauncherTest.java b/src/test/java/spoon/MavenLauncherTest.java index 5046b66d18e..4c61cdc10fe 100644 --- a/src/test/java/spoon/MavenLauncherTest.java +++ b/src/test/java/spoon/MavenLauncherTest.java @@ -18,7 +18,6 @@ public void spoonMavenLauncherTest() { // with the tests launcher = new MavenLauncher("./", MavenLauncher.SOURCE_TYPE.ALL_SOURCE); - assertEquals(14, launcher.getEnvironment().getSourceClasspath().length); // 235 because of the sub folders of src/main/java and src/test/java assertEquals(235, launcher.getModelBuilder().getInputSources().size()); From 60fd19178888eb7f7d647e8774d26d1a6f23feef Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 19 Sep 2017 16:39:47 +0200 Subject: [PATCH 29/33] fix javadoc --- src/main/java/spoon/MavenLauncher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index f0b73075cd2..db6c7c731fd 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -335,7 +335,7 @@ private String getProperty(String key) { } /** - * Get the source version of the project (default 7) + * Get the source version of the project * @return the source version of the project */ public int getSourceVersion() { From afe554dde8987abb7f33471780c6c641682aaea4 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 20 Sep 2017 12:34:24 +0200 Subject: [PATCH 30/33] update doc --- doc/_data/sidebar_doc.yml | 4 +- doc/faq.md | 26 --------- doc/gradle.md | 8 --- doc/launcher.md | 110 ++++++++++++++++++++++---------------- doc/maven.md | 32 ----------- 5 files changed, 66 insertions(+), 114 deletions(-) diff --git a/doc/_data/sidebar_doc.yml b/doc/_data/sidebar_doc.yml index 4891591684f..5f8c2b717c5 100755 --- a/doc/_data/sidebar_doc.yml +++ b/doc/_data/sidebar_doc.yml @@ -82,14 +82,14 @@ entries: product: all version: all - - title: Maven + - title: From Maven Plugin url: /maven.html audience: writers, designers platform: all product: all version: all - - title: Gradle + - title: From Gradle Plugin url: /gradle.html audience: writers, designers platform: all diff --git a/doc/faq.md b/doc/faq.md index ccba7ded76f..74f97121ed9 100644 --- a/doc/faq.md +++ b/doc/faq.md @@ -25,22 +25,6 @@ See . The Spoon metamodel consists of all interfaces that are in packages `spoon.reflect.declaration` (structural part: classes, methods, etc.) and `spoon.reflect.code` (behavioral part: if, loops, etc.). -### How to get a Spoon model programmatically? - -```java -Launcher spoon = new Launcher(); -spoon.addInputResource("src/test/resources/spoon/test/api"); -spoon.run(); -Factory factory = spoon.getFactory(); -// list all packages of the model -for(CtPackage p : factory.Package().getAll()) { - System.out.println("package: "+p.getQualifiedName()); -} -// list all classes of the model -for(CtType s : factory.Class().getAll()) { - System.out.println("class: "+s.getQualifiedName()); -} -``` ## Advanced @@ -81,13 +65,3 @@ final String[] builder = new JDTBuilderImpl() // .build(); ``` -## What are references? How are they handled? - -Spoon analyzes source code. However, this source code may refer to libraries (as a field, parameter, or method return type). Those library may be or not in the classpath. The boundary between source and libraries is handled by the reference mechanism. - -When you're consider a reference object (say, a TypeReference), there are three cases: - -- Case 1: the reference points to a code element for which the source code is present. In this case, reference.getDeclaration() returns this code element (e.g. TypeReference.getDeclaration returns the CtType representing the given java file). reference.getTypeDeclaration() is identical to reference.getDeclaration(). -- Case 2: the reference points to a code element for which the source code is NOT present, but for which the binary class is in the classpath (either the JVM classpath or the --source-classpath argument). In this case, reference.getDeclaration() returns null and reference.getTypeDeclaration returns a partial CtType built using runtime reflection. Those objects built using runtime reflection are called shadow objects; and you can identify them with method isShadow. (This also holds for getFieldDeclaration and getExecutableDeclaration) -- Case 3: : the reference points to a code element for which the source code is NOT present, but for which the binary class is NOT in the classpath. This is called in Spoon the noclasspath mode. In this case, both reference.getDeclaration() and reference.getTypeDeclaration() return null. (This also holds for getFieldDeclaration and getExecutableDeclaration) - diff --git a/doc/gradle.md b/doc/gradle.md index c1a75f4810a..55656fffc72 100644 --- a/doc/gradle.md +++ b/doc/gradle.md @@ -4,14 +4,6 @@ tags: [usage] keywords: gradle, usage, java, plugin --- -## Dependency - -```groovy -compile 'fr.inria.gforge.spoon:spoon-core:{{site.spoon_release}}' -``` - -You may also have to add our repository, see below. - ## Plugin diff --git a/doc/launcher.md b/doc/launcher.md index 65ecb617c2d..2acd0a23efa 100644 --- a/doc/launcher.md +++ b/doc/launcher.md @@ -1,69 +1,87 @@ --- -title: Launcher +title: How to use Spoon in Java? tags: [usage] keywords: usage, java --- -## Dependency +## Basic Launcher -Stable version: +The Spoon `Launcher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/Launcher.html)) is used to create the AST model of a project. -```xml - - fr.inria.gforge.spoon - spoon-core - {{site.spoon_release}} - -``` +```java +Launcher launcher = new Launcher(); -Snapshot version: +// path can be a folder or a file +// addInputResource can be called several times +launcher.addInputResource(""); -```xml - - - fr.inria.gforge.spoon - spoon-core - {{site.spoon_snapshot}} - - - - - gforge.inria.fr-snapshot - Maven Repository for Spoon Snapshot - http://spoon.gforge.inria.fr/repositories/snapshots/ - - - +// if true, the pretty-printed code is readable without fully-qualified names +launcher.getEnvironment().setAutoImports(true); // optional + +// if true, the model can be built even if the dependencies of the analyzed source code are not known or incomplete +// the classes that are in the current classpath are taken into account +launcher.getEnvironment().setNoClasspath(true); // optional + +launcher.buildModel(); +CtModel model = launcher.getModel(); ``` -## Launcher -The Spoon `Launcher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/Launcher.html)) is used to create the AST model of the project. +## Maven Launcher -### Usage +The Spoon `MavenLauncher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html)) is used to create the AST model of a Maven project. +It automatically infers the list of source folders and the dependencies from the `pom.xml` file. +This Launcher handles multi-module Maven projects. ```java -Launcher launcher = new Launcher(); -launcher.addInputResource(""); -launcher.getEnvironment().setAutoImports(true); // optional -launcher.getEnvironment().setComplianceLevel(7); // optional -launcher.getEnvironment().setNoClasspath(true); // optional -launcher.getEnvironment().setSourceClasspath(""); // optional +// the second parameter can be APP_SOURCE / TEST_SOURCE / ALL_SOURCE +MavenLauncher launcher = new MavenLauncher("", MavenLauncher.SOURCE_TYPE.APP_SOURCE); launcher.buildModel(); CtModel model = launcher.getModel(); + +// list all packages of the model +for(CtPackage p : model.getAllPackages()) { + System.out.println("package: "+p.getQualifiedName()); +} +// list all classes of the model +for(CtType s : model.getAllTypes()) { + System.out.println("class: "+s.getQualifiedName()); +} + ``` -## MavenLauncher -The Spoon `MavenLauncher` ([JavaDoc](http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html)) is used to create the AST model of the project by inferring automatically the list of source folder and the dependencies from a Maven `pom.xml` file. -This Launcher simplify the creation of model on complex multi-module project. -If you consider to only use `Processors` in your Spoon, consider to use the [Spoon Maven Plugin](http://spoon.gforge.inria.fr/maven.html) which will do a better job to detect the dependencies. +## About the classpath -### Usage -```java -MavenLauncher launcher = new MavenLauncher("", MavenLauncher.SOURCE_TYPE.APP_SOURCE); -launcher.buildModel(); -CtModel model = launcher.getModel(); -``` \ No newline at end of file +Spoon analyzes source code. However, this source code may refer to libraries (as a field, parameter, or method return type). There are two cases: + +* Full classpath: all dependencies are in the JVM classpath or are given to the Laucher with `launcher.getEnvironment().setSourceClasspath("");` (optional) +* No classpath: some dependencies are unknown and `launcher.getEnvironment().setNoClasspath(true)` is set. + +This has a direct impact on Spoon references. +When you're consider a reference object (say, a TypeReference), there are three cases: + +- Case 1 (code available as source code): the reference points to a code element for which the source code is present. In this case, reference.getDeclaration() returns this code element (e.g. TypeReference.getDeclaration returns the CtType representing the given java file). reference.getTypeDeclaration() is identical to reference.getDeclaration(). +- Case 2 (code available as binary in the classpath): the reference points to a code element for which the source code is NOT present, but for which the binary class is in the classpath (either the JVM classpath or the --source-classpath argument). In this case, reference.getDeclaration() returns null and reference.getTypeDeclaration returns a partial CtType built using runtime reflection. Those objects built using runtime reflection are called shadow objects; and you can identify them with method isShadow. (This also holds for getFieldDeclaration and getExecutableDeclaration) +- Case 3 (code not available, aka noclasspath): the reference points to a code element for which the source code is NOT present, but for which the binary class is NOT in the classpath. This is called in Spoon the noclasspath mode. In this case, both reference.getDeclaration() and reference.getTypeDeclaration() return null. (This also holds for getFieldDeclaration and getExecutableDeclaration) + + +## Declaring the dependency to Spoon + +### Maven + +```xml + + fr.inria.gforge.spoon + spoon-core + {{site.spoon_release}} + +``` + +### Gradle + +```groovy +compile 'fr.inria.gforge.spoon:spoon-core:{{site.spoon_release}}' +``` diff --git a/doc/maven.md b/doc/maven.md index b2a53d26e46..baff2e0f136 100644 --- a/doc/maven.md +++ b/doc/maven.md @@ -4,38 +4,6 @@ tags: [usage] keywords: maven, central, usage, java, plugin --- -## Dependency - -Stable version: - -```xml - - fr.inria.gforge.spoon - spoon-core - {{site.spoon_release}} - -``` - -Snapshot version: - -```xml - - - fr.inria.gforge.spoon - spoon-core - {{site.spoon_snapshot}} - - - - - gforge.inria.fr-snapshot - Maven Repository for Spoon Snapshot - http://spoon.gforge.inria.fr/repositories/snapshots/ - - - -``` - ## Plugin A Maven plugin allows easily launching Spoon when using Maven. This plugin is available in Maven Central From e16900648c50206a7b26efc091d348ba7470f6e6 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 20 Sep 2017 12:49:59 +0200 Subject: [PATCH 31/33] update doc --- doc/gradle.md | 50 ++------------------------------------------------ doc/maven.md | 49 ++----------------------------------------------- 2 files changed, 4 insertions(+), 95 deletions(-) diff --git a/doc/gradle.md b/doc/gradle.md index 55656fffc72..276036f4017 100644 --- a/doc/gradle.md +++ b/doc/gradle.md @@ -4,52 +4,6 @@ tags: [usage] keywords: gradle, usage, java, plugin --- +The main documentation of the Spoon Gradle plugin is in the README of . -## Plugin - -A Gradle plugin allows easily Spoon when using Gradle. -This plugin isn't available in Maven Central yet. Clone the Git project -on your computer and install it on your system. To do that, clone the -official [GitHub project](https://github.com/SpoonLabs/spoon-gradle-plugin) -of this plugin and launch the command line `./gradlew clean install` at -the root directory of the plugin project. After that, use it in your -`build.gradle` files of your project. - - -```groovy -buildscript { - repositories { - mavenLocal() - maven { - url 'http://spoon.gforge.inria.fr/repositories/' - } - } - dependencies { - classpath group: 'fr.inria.gforge.spoon', - name: 'spoon-gradle-plugin', - version:'1.0-SNAPSHOT' - } -} - -apply plugin: 'java' -apply plugin: 'spoon' - -spoon { - processors = ['fr.inria.gforge.spoon.processors.CatchProcessor'] -} -``` - -You simply specify your processors, in fully qualified name, in the configuration -of the plugin, the processors will be applied on your target project before compilation. - -In short, the Gradle plugin gives the classpath of your project to Spoon, -applies Spoon on all source directories and rewrites the transformed Java files in the build -directory. These parameters can be changed in the configuration of the plugin. - -{{site.data.alerts.warning}} -If you want use a processor which isn't in your target project, specify the dependency -as classpath in the buildscript of your project where the Gradle plugin can retrieve it. -Otherwise, your processor won't be applied. -{{site.data.alerts.end}} - -To know more about this Gradle plugin, you can check the README of its [GitHub project](https://github.com/SpoonLabs/spoon-gradle-plugin). +Pull requests on this documentation should be done on and not here. diff --git a/doc/maven.md b/doc/maven.md index baff2e0f136..0f7d03f3f9f 100644 --- a/doc/maven.md +++ b/doc/maven.md @@ -4,51 +4,6 @@ tags: [usage] keywords: maven, central, usage, java, plugin --- -## Plugin +The main documentation of the Spoon Maven plugin is in the README of . -A Maven plugin allows easily launching Spoon when using Maven. This plugin is available in Maven Central -and can be directly inserted in a pom.xml file at the root of a project -(or in the pom.xml of one of a maven module, where you want use Spoon). - -```xml - - fr.inria.gforge.spoon - spoon-maven-plugin - 2.5 - - - generate-sources - - generate - - - - - - fr.inria.gforge.spoon.processors.CatchProcessor - - - - - - fr.inria.gforge.spoon - spoon-core - {{site.spoon_release}} - - - -``` - -You simply specify your processors, in fully qualified name, in the configuration -of the plugin, the processors will be applied on your target project before compilation. - -In short, the Maven plugin gives the classpath of your project to Spoon, -applies Spoon on all source directories and rewrites the transformed Java files in the target -directory. These parameters can be changed in the configuration of the plugin. - -{{site.data.alerts.warning}} -If you want use a processor which isn't in your project, specify the dependency -where the Maven plugin can retrieve it. Otherwise, your processor won't be applied. -{{site.data.alerts.end}} - -To know more about this Maven plugin, check the README of its [GitHub project](https://github.com/SpoonLabs/spoon-maven-plugin). +Pull requests on this documentation should be done on and not here. From c80dc4468b33eb6f56c73f5a9d9fdcbf18d33b14 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 20 Sep 2017 12:54:53 +0200 Subject: [PATCH 32/33] update --- src/main/java/spoon/MavenLauncher.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index db6c7c731fd..105b2f1cfa1 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -72,10 +72,7 @@ public MavenLauncher(String mavenProject, String m2RepositoryPath, SOURCE_TYPE s if (!mavenProjectFile.exists()) { throw new SpoonException(mavenProject + " does not exist."); } - if (mavenProjectFile.isFile()) { - mavenProject = mavenProject.substring(0, mavenProject.length() - mavenProjectFile.getName().length() - 1); - } - + InheritanceModel model; try { model = readPOM(mavenProject, null); From 245f54ece39d8bbe25402dd14d47d665cc8b6763 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 20 Sep 2017 12:59:52 +0200 Subject: [PATCH 33/33] update --- src/main/java/spoon/MavenLauncher.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/spoon/MavenLauncher.java b/src/main/java/spoon/MavenLauncher.java index 105b2f1cfa1..aba83047db3 100644 --- a/src/main/java/spoon/MavenLauncher.java +++ b/src/main/java/spoon/MavenLauncher.java @@ -72,7 +72,7 @@ public MavenLauncher(String mavenProject, String m2RepositoryPath, SOURCE_TYPE s if (!mavenProjectFile.exists()) { throw new SpoonException(mavenProject + " does not exist."); } - + InheritanceModel model; try { model = readPOM(mavenProject, null);