io.freefair.gradle.plugins.maven.plugin.wrappers.MavenProjectWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-plugin-plugin Show documentation
Show all versions of maven-plugin-plugin Show documentation
Gradle Plugin for building Maven Plugins
package io.freefair.gradle.plugins.maven.plugin.wrappers;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.gradle.api.Project;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
/**
* {@link MavenProject} implementation backed by a {@link Project gradle project}.
*
* @author Lars Grefer
*/
public class MavenProjectWrapper extends MavenProject {
private final ProjectLayout projectLayout;
private final File pomFile;
private final SourceSet main;
private final SourceSet test;
public MavenProjectWrapper(ProjectLayout projectLayout, SourceSetContainer sourceSets, File pomFile) throws IOException, XmlPullParserException {
this.projectLayout = projectLayout;
this.pomFile = pomFile;
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader(pomFile));
setModel(model);
getBuild().setDirectory(projectLayout.getBuildDirectory().get().getAsFile().getAbsolutePath());
main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
getBuild().setSourceDirectory(main.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setOutputDirectory(main.getJava().getClassesDirectory().get().getAsFile().getAbsolutePath());
test = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
getBuild().setTestSourceDirectory(test.getJava().getSrcDirs().iterator().next().getAbsolutePath());
getBuild().setTestOutputDirectory(test.getJava().getClassesDirectory().get().getAsFile().getAbsolutePath());
setArtifact(new ProjectArtifact(this));
}
@Override
public File getFile() {
return pomFile;
}
@Override
public File getBasedir() {
return projectLayout.getProjectDirectory().getAsFile();
}
@Override
public List getCompileSourceRoots() {
return main.getJava().getSrcDirs().stream()
.map(File::getAbsolutePath)
.collect(Collectors.toList());
}
@Override
public List getTestCompileSourceRoots() {
return test.getJava().getSrcDirs().stream()
.map(File::getAbsolutePath)
.collect(Collectors.toList());
}
}