Skip to content

Commit

Permalink
Interpolate correctly resource paths in maven project inside maven se…
Browse files Browse the repository at this point in the history
…rver implementation (#5179)

* Interpolate correctly resource paths in maven project inside maven server implementation

* Code refactoring

* Code cleanup

* New line

* Return reading maven attributes from maven project manager

* Code refactoring
  • Loading branch information
Vladyslav Zhukovskii authored May 30, 2017
1 parent 759fef5 commit c2990ae
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ public List<String> getValues(String attributeName) throws ValueStorageException
return readFromPom(attributeName);
}

//Temporary commented due to incorrect resolving maven project attributes from project model, instead of resolving project
//attributes from the maven server, we'll read and parse pom.xml directly by our own.
// final MavenProject mavenProject = mavenProjectManager.getMavenProject(projectFolder.getPath().toString());
// if (mavenProject != null) {
// return getFromMavenProject(mavenProject, attributeName);
// } else {
final MavenProject mavenProject = mavenProjectManager.getMavenProject(projectFolder.getPath().toString());
if (mavenProject != null) {
return getFromMavenProject(mavenProject, attributeName);
} else {
return readFromPom(attributeName);
// }
}
} catch (ServerException | ForbiddenException | IOException e) {
throwReadException(e);
} catch (XMLTreeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void setUp() {
}


@Test(enabled=false)
@Test
public void getArtifactIdFromMavenProject() throws Exception {
String artifactId = NameGenerator.generate("artifactId-", 6);
when(mavenKey.getArtifactId()).thenReturn(artifactId);
Expand All @@ -98,7 +98,7 @@ public void getArtifactIdFromMavenProject() throws Exception {
}


@Test(enabled=false)
@Test
public void getGroupIdFromMavenProject() throws Exception {
String groupId = NameGenerator.generate("groupId-", 6);
when(mavenKey.getGroupId()).thenReturn(groupId);
Expand All @@ -112,7 +112,7 @@ public void getGroupIdFromMavenProject() throws Exception {
}


@Test(enabled=false)
@Test
public void getVersionFromMavenProject() throws Exception {
String versionId = NameGenerator.generate("version-", 6);
when(mavenKey.getVersion()).thenReturn(versionId);
Expand All @@ -126,7 +126,7 @@ public void getVersionFromMavenProject() throws Exception {
}


@Test(enabled=false)
@Test
public void getPackagingFromMavenProject() throws Exception {
when(mavenProjectManager.getMavenProject(anyString())).thenReturn(mavenProject);
when(mavenProject.getPackaging()).thenReturn("war");
Expand All @@ -138,7 +138,7 @@ public void getPackagingFromMavenProject() throws Exception {
Assert.assertEquals(pkgs.get(0), "war");
}

@Test(enabled=false)
@Test
public void getPackagingFromMavenProjectIfNotSet() throws Exception {
when(mavenProjectManager.getMavenProject(anyString())).thenReturn(mavenProject);
List<String> pkgs = mavenValueProvider.getValues(MavenAttributes.PACKAGING);
Expand All @@ -150,7 +150,7 @@ public void getPackagingFromMavenProjectIfNotSet() throws Exception {
}


@Test(enabled=false)
@Test
public void getParentArtifactFromMavenProject() throws Exception {
String parentArtifact = NameGenerator.generate("parentArtifact", 6);
when(parentKey.getArtifactId()).thenReturn(parentArtifact);
Expand All @@ -164,7 +164,7 @@ public void getParentArtifactFromMavenProject() throws Exception {
}


@Test(enabled=false)
@Test
public void getParentVersionFromMavenProject() throws Exception {
String parentVersionId = NameGenerator.generate("parent-version-", 6);
when(parentKey.getVersion()).thenReturn(parentVersionId);
Expand All @@ -177,7 +177,7 @@ public void getParentVersionFromMavenProject() throws Exception {
Assert.assertEquals(versions.get(0), parentVersionId);
}

@Test(enabled=false)
@Test
public void getParentGroupFromMavenProject() throws Exception {
String groupId = NameGenerator.generate("parent-group-", 6);
when(parentKey.getGroupId()).thenReturn(groupId);
Expand All @@ -190,7 +190,7 @@ public void getParentGroupFromMavenProject() throws Exception {
Assert.assertEquals(values.get(0), groupId);
}

@Test(enabled=false)
@Test
public void getSourceFromMavenProject() throws Exception {
final List<String> strings = singletonList("src");
when(mavenProject.getSources()).thenReturn(strings);
Expand All @@ -202,7 +202,7 @@ public void getSourceFromMavenProject() throws Exception {
Assert.assertEquals(sources, strings);
}

@Test(enabled=false)
@Test
public void getSourceFromMavenProjectIfNotSet() throws Exception {
when(mavenProjectManager.getMavenProject(anyString())).thenReturn(mavenProject);
List<String> sources = mavenValueProvider.getValues(Constants.SOURCE_FOLDER);
Expand All @@ -212,7 +212,7 @@ public void getSourceFromMavenProjectIfNotSet() throws Exception {
Assert.assertEquals(sources, singletonList(MavenAttributes.DEFAULT_SOURCE_FOLDER));
}

@Test(enabled=false)
@Test
public void getTestSourceFromMavenProject() throws Exception {
List<String> strings = singletonList("src/test");
when(mavenProject.getTestSources()).thenReturn(strings);
Expand All @@ -224,7 +224,7 @@ public void getTestSourceFromMavenProject() throws Exception {
Assert.assertEquals(sources, strings);
}

@Test(enabled=false)
@Test
public void getTestSourceFromMavenProjectIfNotSet() throws Exception {
when(mavenProjectManager.getMavenProject(anyString())).thenReturn(mavenProject);
List<String> sources = mavenValueProvider.getValues(MavenAttributes.TEST_SOURCE_FOLDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
* Util methods for converting maven model objects into maven-server objects
Expand All @@ -69,7 +70,7 @@ public static MavenKey keyFor(Artifact artifact) {
return new MavenKey(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion());
}

public static MavenModel convertModel(Model model) {
public static MavenModel convertModel(Model model, File projectDir) {
Build build = model.getBuild();
List<String> sources = new ArrayList<>();
List<String> testSources = new ArrayList<>();
Expand All @@ -84,10 +85,10 @@ public static MavenModel convertModel(Model model) {
}
}

return convertModel(model, sources, testSources, Collections.emptyList(), Collections.emptyList(), null);
return convertModel(model, projectDir, sources, testSources, Collections.emptyList(), Collections.emptyList(), null);
}

public static MavenModel convertModel(Model model, List<String> sources, List<String> testSources, Collection<Artifact> dependencies,
public static MavenModel convertModel(Model model, File projectDir, List<String> sources, List<String> testSources, Collection<Artifact> dependencies,
Collection<Artifact> extensions, File localRepo) {

MavenModel result = new MavenModel();
Expand All @@ -111,28 +112,32 @@ public static MavenModel convertModel(Model model, List<String> sources, List<St
result.setDependencies(convertArtifacts(dependencies, convertedArtifacts, localRepo));

result.setRemoteRepositories(convertRepositories(model.getRepositories()));
result.setProfiles(convertProfiles(model.getProfiles()));
convertBuild(result.getBuild(), model.getBuild(), sources, testSources);
result.setProfiles(convertProfiles(model.getProfiles(), projectDir));
convertBuild(result.getBuild(), model.getBuild(), projectDir, sources, testSources);

return result;
}

public static MavenModel convertProjectToModel(MavenProject project, List<DependencyNode> dependencyNodes, File localRepository) {
Model model = project.getModel();
return convertModel(model, project.getCompileSourceRoots(), project.getTestCompileSourceRoots(), project.getArtifacts(),
return convertModel(model, project.getBasedir(), project.getCompileSourceRoots(), project.getTestCompileSourceRoots(), project.getArtifacts(),
project.getExtensionArtifacts(), localRepository);
}

private static void convertBuild(MavenBuild mavenBuild, Build build, List<String> compileSourceRoots,
private static void convertBuild(MavenBuild mavenBuild, Build build, File projectDir, List<String> compileSourceRoots,
List<String> testCompileSourceRoots) {
convertBaseBuild(build, mavenBuild);
mavenBuild.setOutputDirectory(build.getOutputDirectory());
mavenBuild.setTestOutputDirectory(build.getTestOutputDirectory());
mavenBuild.setSources(compileSourceRoots);
mavenBuild.setTestSources(testCompileSourceRoots);
}

private static List<MavenProfile> convertProfiles(List<Profile> profiles) {
convertBaseBuild(build, mavenBuild, projectDir);
mavenBuild.setOutputDirectory(relativize(projectDir, build.getOutputDirectory()));
mavenBuild.setTestOutputDirectory(relativize(projectDir, build.getTestOutputDirectory()));
mavenBuild.setSources(compileSourceRoots.stream()
.map(path -> relativize(projectDir, path))
.collect(toList()));
mavenBuild.setTestSources(testCompileSourceRoots.stream()
.map(path -> relativize(projectDir, path))
.collect(toList()));
}

private static List<MavenProfile> convertProfiles(List<Profile> profiles, File projectDir) {
List<MavenProfile> result = new ArrayList<>();

if (profiles != null) {
Expand All @@ -151,7 +156,7 @@ private static List<MavenProfile> convertProfiles(List<Profile> profiles) {

mavenProfile.setActivation(convertActivation(profile.getActivation()));
if (profile.getBuild() != null) {
convertBaseBuild(profile.getBuild(), mavenProfile.getBuild());
convertBaseBuild(profile.getBuild(), mavenProfile.getBuild(), projectDir);
}
result.add(mavenProfile);
}
Expand All @@ -160,12 +165,12 @@ private static List<MavenProfile> convertProfiles(List<Profile> profiles) {
return result;
}

private static void convertBaseBuild(BuildBase build, MavenBuildBase mavenBuild) {
private static void convertBaseBuild(BuildBase build, MavenBuildBase mavenBuild, File projectDir) {
mavenBuild.setDefaultGoal(build.getDefaultGoal());
mavenBuild.setDirectory(build.getDirectory());
mavenBuild.setDirectory(relativize(projectDir, build.getDirectory()));
mavenBuild.setFinalName(build.getFinalName());
mavenBuild.setResources(convenrtResources(build.getResources()));
mavenBuild.setTestResources(convenrtResources(build.getTestResources()));
mavenBuild.setResources(convenrtResources(build.getResources(), projectDir));
mavenBuild.setTestResources(convenrtResources(build.getTestResources(), projectDir));
List<String> filters = build.getFilters();
if (filters == null) {
mavenBuild.setFilters(Collections.emptyList());
Expand All @@ -174,11 +179,11 @@ private static void convertBaseBuild(BuildBase build, MavenBuildBase mavenBuild)
}
}

private static List<MavenResource> convenrtResources(List<Resource> resources) {
private static List<MavenResource> convenrtResources(List<Resource> resources, File projectDir) {
List<MavenResource> result = new ArrayList<>();
if (resources != null) {
for (Resource res : resources) {
result.add(new MavenResource(res.getDirectory(),
result.add(new MavenResource(relativize(projectDir, res.getDirectory()),
res.isFiltering(),
res.getTargetPath(),
patternsOrEmptyList(res.getIncludes()),
Expand Down Expand Up @@ -262,7 +267,7 @@ private static List<MavenArtifact> convertArtifacts(Collection<Artifact> artifac
ArrayList<MavenArtifact> result = new ArrayList<>();
if (artifacts != null) {
result.addAll(artifacts.stream().map(artifact -> convertArtifact(artifact, convertedArtifacts, localRepository))
.collect(Collectors.toList()));
.collect(toList()));
}

return result;
Expand Down Expand Up @@ -304,7 +309,7 @@ private static List<MavenPlugin> convertPlugins(Model model) {
if (build != null) {
List<Plugin> plugins = build.getPlugins();
if (plugins != null) {
result.addAll(plugins.stream().map(MavenModelUtil::convertPlugin).collect(Collectors.toList()));
result.addAll(plugins.stream().map(MavenModelUtil::convertPlugin).collect(toList()));
}
}

Expand All @@ -314,13 +319,13 @@ private static List<MavenPlugin> convertPlugins(Model model) {
private static MavenPlugin convertPlugin(Plugin plugin) {
List<MavenPluginExecution> executions =
plugin.getExecutions().stream().map(MavenModelUtil::convertExecution)
.collect(Collectors.toList());
.collect(toList());

List<MavenKey> dependecies =
plugin.getDependencies()
.stream()
.map(dependency -> new MavenKey(dependency.getGroupId(), dependency.getArtifactId(),
dependency.getVersion())).collect(Collectors.toList());
dependency.getVersion())).collect(toList());

return new MavenPlugin(plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion(), false,
convertConfiguration(plugin.getConfiguration()), executions, dependecies);
Expand Down Expand Up @@ -358,7 +363,7 @@ private static MavenPluginExecution convertExecution(PluginExecution execution)
return new MavenPluginExecution(execution.getId(), convertConfiguration(execution.getConfiguration()), execution.getGoals());
}

public static Model convertToMavenModel(MavenModel model) {
public static Model convertToMavenModel(MavenModel model, File projectDir) {
Model result = new Model();
result.setArtifactId(model.getMavenKey().getArtifactId());
result.setGroupId(model.getMavenKey().getGroupId());
Expand All @@ -380,27 +385,27 @@ public static Model convertToMavenModel(MavenModel model) {
result.setModules(model.getModules());
result.setBuild(new Build());
MavenBuild modelBuild = model.getBuild();
convertToMavenBuildBase(modelBuild, result.getBuild());
result.getBuild().setSourceDirectory(modelBuild.getSources().get(0));
result.getBuild().setTestSourceDirectory(modelBuild.getTestSources().get(0));
convertToMavenBuildBase(modelBuild, result.getBuild(), projectDir);
result.getBuild().setSourceDirectory(relativize(projectDir, modelBuild.getSources().get(0)));
result.getBuild().setTestSourceDirectory(relativize(projectDir, modelBuild.getTestSources().get(0)));

result.setProfiles(convertToMavenProfiles(model.getProfiles()));
result.setProfiles(convertToMavenProfiles(model.getProfiles(), projectDir));
return result;
}

private static List<Profile> convertToMavenProfiles(List<MavenProfile> profiles) {
return profiles.stream().map(MavenModelUtil::convertToMavenProfile).collect(Collectors.toList());
private static List<Profile> convertToMavenProfiles(List<MavenProfile> profiles, File projectDir) {
return profiles.stream().map(profile -> convertToMavenProfile(profile, projectDir)).collect(toList());
}

private static Profile convertToMavenProfile(MavenProfile mavenProfile) {
private static Profile convertToMavenProfile(MavenProfile mavenProfile, File projectDir) {
Profile result = new Profile();
result.setId(mavenProfile.getId());
result.setSource(mavenProfile.getSource());
result.setModules(mavenProfile.getModules());
result.setProperties(mavenProfile.getProperties());
result.setBuild(new Build());
result.setActivation(convertToMavenActivation(mavenProfile.getActivation()));
convertToMavenBuildBase(mavenProfile.getBuild(), result.getBuild());
convertToMavenBuildBase(mavenProfile.getBuild(), result.getBuild(), projectDir);
return result;
}

Expand Down Expand Up @@ -449,22 +454,22 @@ private static ActivationFile convertToMavenActivationFile(MavenActivationFile f
return null;
}

private static void convertToMavenBuildBase(MavenBuildBase modelBuild, BuildBase build) {
private static void convertToMavenBuildBase(MavenBuildBase modelBuild, BuildBase build, File projectDir) {
build.setFinalName(modelBuild.getFinalName());
build.setDefaultGoal(modelBuild.getDefaultGoal());
build.setDirectory(modelBuild.getDirectory());
build.setDirectory(relativize(projectDir, modelBuild.getDirectory()));
build.setFilters(modelBuild.getFilters());
build.setResources(convertToMavenResources(modelBuild.getResources()));
build.setTestResources(convertToMavenResources(modelBuild.getTestResources()));
build.setResources(convertToMavenResources(modelBuild.getResources(), projectDir));
build.setTestResources(convertToMavenResources(modelBuild.getTestResources(), projectDir));
}

private static List<Resource> convertToMavenResources(List<MavenResource> resources) {
return resources.stream().map(MavenModelUtil::convertToMavenResource).collect(Collectors.toList());
private static List<Resource> convertToMavenResources(List<MavenResource> resources, File projectDir) {
return resources.stream().map(resource -> convertToMavenResource(resource, projectDir)).collect(toList());
}

private static Resource convertToMavenResource(MavenResource mavenResource) {
private static Resource convertToMavenResource(MavenResource mavenResource, File projectDir) {
Resource resource = new Resource();
resource.setDirectory(mavenResource.getDirectory());
resource.setDirectory(relativize(projectDir, mavenResource.getDirectory()));
resource.setFiltering(mavenResource.isFiltered());
resource.setTargetPath(mavenResource.getTargetPath());
resource.setIncludes(mavenResource.getIncludes());
Expand Down Expand Up @@ -499,4 +504,16 @@ private static RepositoryPolicy convertToMavenPolicy(MavenRepositoryPolicy polic
result.setUpdatePolicy(policy.getUpdatePolicy());
return result;
}

private static String relativize(File basePath, String rawPath) {
if (rawPath == null) {
return null;
}

if (rawPath.isEmpty()) {
return rawPath;
}

return rawPath.startsWith(File.separator) ? basePath.toURI().relativize(new File(rawPath).toURI()).getPath() : rawPath;
}
}
Loading

0 comments on commit c2990ae

Please sign in to comment.