use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class AttachModelArchive method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Model model = ProjectHelper.getRawModel(this.project);
if (usePomVariables) {
model = ModelUtility.applyVariables(model, new PomVariableResolver(project));
}
if (usePomDependencies) {
model = ModelUtility.applyArtifactVersions(model, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies));
}
// write the model archive
final File outputFile = new File(this.project.getBuild().getDirectory() + File.separatorChar + modelArchiveName + "." + ModelArchiveWriter.DEFAULT_EXTENSION);
outputFile.getParentFile().mkdirs();
try (final FileOutputStream fos = new FileOutputStream(outputFile)) {
// TODO provide base manifest
final JarOutputStream jos = ModelArchiveWriter.write(fos, model, null, new ModelArchiveWriter.ArtifactProvider() {
@Override
public InputStream getInputStream(final Artifact artifact) throws IOException {
try {
final org.apache.maven.artifact.Artifact a = ModelUtils.getArtifact(project, mavenSession, artifactHandlerManager, resolver, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier());
return new FileInputStream(a.getFile());
} catch (MojoExecutionException e) {
throw (IOException) new IOException("Unable to get artifact: " + artifact.toMvnUrl()).initCause(e);
}
}
});
// handle license etc.
final File classesDir = new File(this.project.getBuild().getOutputDirectory());
if (classesDir.exists()) {
final File metaInfDir = new File(classesDir, "META-INF");
for (final String name : new String[] { "LICENSE", "NOTICE", "DEPENDENCIES" }) {
final File f = new File(metaInfDir, name);
if (f.exists()) {
final JarEntry artifactEntry = new JarEntry("META-INF/" + name);
jos.putNextEntry(artifactEntry);
final byte[] buffer = new byte[8192];
try (final InputStream is = new FileInputStream(f)) {
int l = 0;
while ((l = is.read(buffer)) > 0) {
jos.write(buffer, 0, l);
}
}
jos.closeEntry();
}
}
}
jos.finish();
} catch (final IOException e) {
throw new MojoExecutionException("Unable to write model archive to " + outputFile + " : " + e.getMessage(), e);
}
// attach it as an additional artifact
projectHelper.attachArtifact(project, ModelArchiveWriter.DEFAULT_EXTENSION, BuildConstants.CLASSIFIER_MAR, outputFile);
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class AttachSlingStartModel method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Model model = ProjectHelper.getRawModel(this.project);
if (usePomVariables) {
model = ModelUtility.applyVariables(model, new PomVariableResolver(project));
}
if (usePomDependencies) {
model = ModelUtility.applyArtifactVersions(model, new PomArtifactVersionResolver(project, allowUnresolvedPomDependencies));
}
// write the model
final File outputFile = new File(this.project.getBuild().getDirectory() + File.separatorChar + BuildConstants.MODEL_ARTIFACT_NAME);
outputFile.getParentFile().mkdirs();
try (final Writer writer = new FileWriter(outputFile)) {
ModelWriter.write(writer, model);
} catch (final IOException e) {
throw new MojoExecutionException("Unable to write model to " + outputFile, e);
}
// if this project is a partial bundle list, it's the main artifact
if (project.getPackaging().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM)) {
project.getArtifact().setFile(outputFile);
} else {
// otherwise attach it as an additional artifact
projectHelper.attachArtifact(project, BuildConstants.PACKAGING_PARTIAL_SYSTEM, BuildConstants.CLASSIFIER_PARTIAL_SYSTEM, outputFile);
}
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class ModelPreprocessor method readLocalModel.
/**
* Read all model files from the directory in alphabetical order.
* Only files ending with .txt or .model are read.
*
* @param project The current maven project
* @param inlinedModel the inlined model to be merged with the models in modelDirectory (may be null)
* @param modelDirectory The directory to scan for models
* @param pattern Pattern used to find the textual models within the modelDirectory
* @param logger The logger
*/
protected Model readLocalModel(final MavenProject project, final String inlinedModel, final File modelDirectory, final String pattern, final Logger logger) throws MavenExecutionException, IOException {
final Pattern p = Pattern.compile(pattern);
final List<String> candidates = new ArrayList<>();
if (modelDirectory != null && modelDirectory.exists()) {
for (final File f : modelDirectory.listFiles()) {
if (f.isFile() && !f.getName().startsWith(".")) {
if (p.matcher(f.getName()).matches()) {
candidates.add(f.getName());
}
}
}
Collections.sort(candidates);
}
if (candidates.size() == 0 && (inlinedModel == null || inlinedModel.trim().length() == 0)) {
throw new MavenExecutionException("No model files found in " + modelDirectory + ", and no model inlined in POM.", (File) null);
}
final Model result = new Model();
if (inlinedModel != null) {
logger.debug("Reading inlined model from project " + project.getId());
try {
final Reader reader = new StringReader(inlinedModel);
try {
final Model current = ModelReader.read(reader, "pom");
final Map<Traceable, String> errors = ModelUtility.validate(current);
if (errors != null) {
throw new MavenExecutionException("Invalid inlined model : " + errors, (File) null);
}
MergeUtility.merge(result, current, new MergeUtility.MergeOptions().setHandleRemoveRunMode(false));
} finally {
IOUtils.closeQuietly(reader);
}
} catch (final IOException io) {
throw new MavenExecutionException("Unable to read inlined model", io);
}
}
for (final String name : candidates) {
logger.debug("Reading model " + name + " in project " + project.getId());
try {
final File f = new File(modelDirectory, name);
final FileReader reader = new FileReader(f);
try {
final Model current = ModelReader.read(reader, f.getAbsolutePath());
final Map<Traceable, String> errors = ModelUtility.validate(current);
if (errors != null) {
throw new MavenExecutionException("Invalid model at " + name + " : " + errors, (File) null);
}
MergeUtility.merge(result, current, new MergeUtility.MergeOptions().setHandleRemoveRunMode(false));
} finally {
IOUtils.closeQuietly(reader);
}
} catch (final IOException io) {
throw new MavenExecutionException("Unable to read model at " + name, io);
}
}
final Map<Traceable, String> errors = ModelUtility.validate(result);
if (errors != null) {
throw new MavenExecutionException("Invalid assembled model : " + errors, (File) null);
}
return postProcessReadModel(result);
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class ModelPreprocessor method searchSlingstartDependencies.
/**
* Search for dependent slingstart/slingfeature artifacts and remove them from the effective model.
* @throws MavenExecutionException
*/
private List<Model> searchSlingstartDependencies(final Environment env, final ProjectInfo info, final Model rawModel, final Model effectiveModel) throws MavenExecutionException {
// slingstart or slingfeature
final List<Model> dependencies = new ArrayList<>();
for (final Feature feature : effectiveModel.getFeatures()) {
for (final RunMode runMode : feature.getRunModes()) {
for (final ArtifactGroup group : runMode.getArtifactGroups()) {
final List<org.apache.sling.provisioning.model.Artifact> removeList = new ArrayList<>();
for (final org.apache.sling.provisioning.model.Artifact a : group) {
if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART) || a.getType().equals(BuildConstants.PACKAGING_PARTIAL_SYSTEM)) {
final Dependency dep = new Dependency();
dep.setGroupId(a.getGroupId());
dep.setArtifactId(a.getArtifactId());
dep.setVersion(a.getVersion());
dep.setType(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
if (a.getType().equals(BuildConstants.PACKAGING_SLINGSTART)) {
dep.setClassifier(BuildConstants.PACKAGING_PARTIAL_SYSTEM);
} else {
dep.setClassifier(a.getClassifier());
}
dep.setScope(Artifact.SCOPE_PROVIDED);
env.logger.debug("- adding dependency " + ModelUtils.toString(dep));
info.project.getDependencies().add(dep);
// if it's a project from the current reactor build, we can't resolve it right now
final String key = a.getGroupId() + ":" + a.getArtifactId();
final ProjectInfo depInfo = env.modelProjects.get(key);
if (depInfo != null) {
env.logger.debug("Found reactor " + a.getType() + " dependency : " + a);
final Model model = addDependencies(env, depInfo);
if (model == null) {
throw new MavenExecutionException("Recursive model dependency list including project " + info.project, (File) null);
}
dependencies.add(model);
info.includedModels.put(a, depInfo.localModel);
} else {
env.logger.debug("Found external " + a.getType() + " dependency: " + a);
// "external" dependency, we can already resolve it
final File modelFile = resolveSlingstartArtifact(env, info.project, dep);
FileReader r = null;
try {
r = new FileReader(modelFile);
final Model model = ModelReader.read(r, modelFile.getAbsolutePath());
info.includedModels.put(a, model);
final Map<Traceable, String> errors = ModelUtility.validate(model);
if (errors != null) {
throw new MavenExecutionException("Unable to read model file from " + modelFile + " : " + errors, modelFile);
}
final Model fullModel = processSlingstartDependencies(env, info, dep, model);
dependencies.add(fullModel);
} catch (final IOException ioe) {
throw new MavenExecutionException("Unable to read model file from " + modelFile, ioe);
} finally {
try {
if (r != null) {
r.close();
}
} catch (final IOException io) {
// ignore
}
}
}
removeList.add(a);
}
}
for (final org.apache.sling.provisioning.model.Artifact r : removeList) {
group.remove(r);
final Feature localModelFeature = rawModel.getFeature(feature.getName());
if (localModelFeature != null) {
final RunMode localRunMode = localModelFeature.getRunMode(runMode.getNames());
if (localRunMode != null) {
final ArtifactGroup localAG = localRunMode.getArtifactGroup(group.getStartLevel());
if (localAG != null) {
localAG.remove(r);
}
}
}
}
}
}
}
return dependencies;
}
use of org.apache.sling.provisioning.model.Model in project sling by apache.
the class ModelPreprocessor method addDependencies.
/**
* Add dependencies for a single project.
* @param env The environment with all maven settings and projects
* @param info The project to process.
* @throws MavenExecutionException If anything goes wrong.
*/
private Model addDependencies(final Environment env, final ProjectInfo info) throws MavenExecutionException {
if (info.done == true) {
env.logger.debug("Return prepared model for " + info.project);
return info.model;
}
// prevent recursion and multiple processing
info.done = true;
env.logger.debug("Processing project " + info.project);
// read local model
final String pattern = nodeValue(info.plugin, "modelPattern", AbstractSlingStartMojo.DEFAULT_MODEL_PATTERN);
final String inlinedModel = nodeValue(info.plugin, "model", null);
String scope = Artifact.SCOPE_PROVIDED;
try {
if (hasNodeValue(info.plugin, "modelDirectory")) {
final String directory = nodeValue(info.plugin, "modelDirectory", null);
info.localModel = readLocalModel(info.project, inlinedModel, new File(directory), pattern, env.logger);
} else {
// use multiple fallbacks here only in case the default model directory is not explicitly set
File defaultModelDirectory = new File(info.project.getBasedir(), "src/main/provisioning");
if (defaultModelDirectory.exists()) {
env.logger.debug("Try to extract model from default provisioning directory " + defaultModelDirectory.getAbsolutePath());
info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectory, pattern, env.logger);
} else {
File defaultModelDirectoryInTest = new File(info.project.getBasedir(), "src/test/provisioning");
env.logger.debug("Try to extract model from default test provisioning directory " + defaultModelDirectoryInTest.getAbsolutePath());
info.localModel = readLocalModel(info.project, inlinedModel, defaultModelDirectoryInTest, pattern, env.logger);
scope = Artifact.SCOPE_TEST;
}
}
} catch (final IOException ioe) {
throw new MavenExecutionException(ioe.getMessage(), ioe);
}
// process attachments
processAttachments(env, info);
// is the maven classpath supposed to be extended?
info.extendMavenClassPath = !nodeBooleanValue(info.plugin, AbstractSlingStartMojo.CONFIGURATION_NAME_DISABLE_EXTENDING_CLASSPATH, false);
// check for setting version
if (nodeBooleanValue(info.plugin, "setFeatureVersions", false)) {
for (final Feature f : info.localModel.getFeatures()) {
if (f.getVersion() == null) {
f.setVersion(cleanupVersion(info.project.getVersion()));
}
}
}
// prepare resolver options
ResolverOptions resolverOptions = new ResolverOptions();
if (nodeBooleanValue(info.plugin, "usePomVariables", false)) {
resolverOptions.variableResolver(new PomVariableResolver(info.project));
}
if (nodeBooleanValue(info.plugin, "usePomDependencies", false)) {
resolverOptions.artifactVersionResolver(new PomArtifactVersionResolver(info.project, nodeBooleanValue(info.plugin, "allowUnresolvedPomDependencies", false)));
}
final Model copyModel = new Model();
this.mergeModels(copyModel, info.localModel);
// we have to create an effective model to add the dependencies
final Model effectiveModel = ModelUtility.getEffectiveModel(copyModel, resolverOptions);
final List<Model> dependencies = searchSlingstartDependencies(env, info, copyModel, effectiveModel);
info.model = new Model();
for (final Model d : dependencies) {
this.mergeModels(info.model, d);
}
this.mergeModels(info.model, copyModel);
info.model = ModelUtility.getEffectiveModel(info.model, resolverOptions);
final Map<Traceable, String> errors = ModelUtility.validate(info.model);
if (errors != null) {
throw new MavenExecutionException("Unable to create model file for " + info.project + " : " + errors, (File) null);
}
if (info.extendMavenClassPath) {
addDependenciesFromModel(env, info, scope);
env.logger.info("Extended Maven classpath (scope '" + scope + "') by the dependencies extracted from the Sling model.");
} else {
env.logger.debug("Do not enrich Maven classpath with Sling model!");
}
try {
ProjectHelper.storeProjectInfo(info);
} catch (final IOException ioe) {
throw new MavenExecutionException(ioe.getMessage(), ioe);
}
return info.model;
}
Aggregations