use of org.apache.maven.model.Resource in project maven-plugins by apache.
the class AntBuildWriter method writeCompileTasks.
private void writeCompileTasks(XMLWriter writer, String outputDirectory, List compileSourceRoots, List resources, String additionalClassesDirectory, boolean isTest) throws IOException {
writer.startElement("mkdir");
writer.addAttribute("dir", outputDirectory);
// mkdir
writer.endElement();
// CHECKSTYLE_OFF: LineLength
if (!compileSourceRoots.isEmpty()) {
writer.startElement("javac");
writer.addAttribute("destdir", outputDirectory);
Map[] includes = AntBuildWriterUtil.getMavenCompilerPluginOptions(project, "includes", null);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "includes", getCommaSeparatedList(includes, "include"), 3);
Map[] excludes = AntBuildWriterUtil.getMavenCompilerPluginOptions(project, "excludes", null);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "excludes", getCommaSeparatedList(excludes, "exclude"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "encoding", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "encoding", null), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "nowarn", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "showWarnings", "false"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "debug", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "debug", "true"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "optimize", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "optimize", "false"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "deprecation", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "showDeprecation", "true"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "target", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "target", "1.1"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "verbose", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "verbose", "false"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "fork", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "fork", "false"), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "memoryMaximumSize", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "meminitial", null), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "memoryInitialSize", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "maxmem", null), 3);
AntBuildWriterUtil.addWrapAttribute(writer, "javac", "source", AntBuildWriterUtil.getMavenCompilerPluginBasicOption(project, "source", "1.3"), 3);
String[] compileSourceRootsArray = (String[]) compileSourceRoots.toArray(new String[compileSourceRoots.size()]);
for (int i = 0; i < compileSourceRootsArray.length; i++) {
writer.startElement("src");
writer.startElement("pathelement");
if (isTest) {
writer.addAttribute("location", "${maven.build.testDir." + i + "}");
} else {
writer.addAttribute("location", "${maven.build.srcDir." + i + "}");
}
// pathelement
writer.endElement();
// src
writer.endElement();
}
if (additionalClassesDirectory == null) {
writer.startElement("classpath");
if (isTest) {
writer.addAttribute("refid", "build.test.classpath");
} else {
writer.addAttribute("refid", "build.classpath");
}
// classpath
writer.endElement();
} else {
writer.startElement("classpath");
writer.startElement("path");
if (isTest) {
writer.addAttribute("refid", "build.test.classpath");
} else {
writer.addAttribute("refid", "build.classpath");
}
// path
writer.endElement();
writer.startElement("pathelement");
writer.addAttribute("location", additionalClassesDirectory);
// pathelement
writer.endElement();
// classpath
writer.endElement();
}
// javac
writer.endElement();
}
// CHECKSTYLE_ON: LineLength
Resource[] array = (Resource[]) resources.toArray(new Resource[resources.size()]);
for (int i = 0; i < array.length; i++) {
Resource resource = array[i];
if (new File(resource.getDirectory()).exists()) {
String outputDir = outputDirectory;
if (resource.getTargetPath() != null && resource.getTargetPath().length() > 0) {
outputDir = outputDir + "/" + resource.getTargetPath();
writer.startElement("mkdir");
writer.addAttribute("dir", outputDir);
// mkdir
writer.endElement();
}
writer.startElement("copy");
writer.addAttribute("todir", outputDir);
writer.startElement("fileset");
if (isTest) {
writer.addAttribute("dir", "${maven.build.testResourceDir." + i + "}");
} else {
writer.addAttribute("dir", "${maven.build.resourceDir." + i + "}");
}
AntBuildWriterUtil.writeIncludesExcludes(writer, resource.getIncludes(), resource.getExcludes());
// fileset
writer.endElement();
// copy
writer.endElement();
}
}
}
use of org.apache.maven.model.Resource in project maven-plugins by apache.
the class JavadocJar method generateArchive.
// ----------------------------------------------------------------------
// private methods
// ----------------------------------------------------------------------
/**
* Method that creates the jar file
*
* @param javadocFiles the directory where the generated jar file will be put
* @param jarFileName the filename of the generated jar file
* @return a File object that contains the generated jar file
* @throws ArchiverException {@link ArchiverException}
* @throws IOException {@link IOException}
*/
private File generateArchive(File javadocFiles, String jarFileName) throws ArchiverException, IOException {
File javadocJar = new File(jarOutputDirectory, jarFileName);
if (javadocJar.exists()) {
javadocJar.delete();
}
MavenArchiver archiver = new MavenArchiver();
archiver.setArchiver(jarArchiver);
archiver.setOutputFile(javadocJar);
File contentDirectory = javadocFiles;
if (!contentDirectory.exists()) {
getLog().warn("JAR will be empty - no content was marked for inclusion!");
} else {
archiver.getArchiver().addDirectory(contentDirectory, DEFAULT_INCLUDES, DEFAULT_EXCLUDES);
}
List<Resource> resources = project.getBuild().getResources();
for (Resource r : resources) {
if (r.getDirectory().endsWith("maven-shared-archive-resources")) {
archiver.getArchiver().addDirectory(new File(r.getDirectory()));
}
}
if (useDefaultManifestFile && defaultManifestFile.exists() && archive.getManifestFile() == null) {
getLog().info("Adding existing MANIFEST to archive. Found under: " + defaultManifestFile.getPath());
archive.setManifestFile(defaultManifestFile);
}
try {
archiver.createArchive(session, project, archive);
} catch (ManifestException e) {
throw new ArchiverException("ManifestException: " + e.getMessage(), e);
} catch (DependencyResolutionRequiredException e) {
throw new ArchiverException("DependencyResolutionRequiredException: " + e.getMessage(), e);
}
return javadocJar;
}
use of org.apache.maven.model.Resource in project karaf by apache.
the class GenerateServiceMetadata method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
try {
boolean addSourceDirectory = false;
StringBuilder requirements = new StringBuilder();
StringBuilder capabilities = new StringBuilder();
ClassFinder finder = createFinder(classLoader);
List<Class<?>> classes = finder.findAnnotatedClasses(Services.class);
List<Class<?>> activators = new ArrayList<>();
for (Class<?> clazz : classes) {
URL classUrl = clazz.getClassLoader().getResource(clazz.getName().replace('.', '/') + ".class");
URL outputDirectoryUrl = new File(project.getBuild().getOutputDirectory()).toURI().toURL();
if (classUrl == null || !classUrl.getPath().startsWith(outputDirectoryUrl.getPath())) {
System.out.println("Ignoring " + classUrl);
continue;
}
if (BundleActivator.class.isAssignableFrom(clazz)) {
activators.add(clazz);
}
Properties props = new Properties();
Services services = clazz.getAnnotation(Services.class);
if (services != null) {
for (RequireService req : services.requires()) {
String fltWithClass = combine(req.filter(), "(objectClass=" + req.value().getName() + ")");
addServiceReq(requirements, fltWithClass);
props.setProperty(req.value().getName(), req.filter());
}
for (ProvideService cap : services.provides()) {
addServiceCap(capabilities, cap);
}
}
Managed managed = clazz.getAnnotation(Managed.class);
if (managed != null) {
props.setProperty("pid", managed.value());
}
File file = new File(outputDirectory, "OSGI-INF/karaf-tracker/" + clazz.getName());
file.getParentFile().mkdirs();
try (OutputStream os = buildContext.newFileOutputStream(file)) {
props.store(os, null);
}
addSourceDirectory = true;
}
if (addSourceDirectory) {
Resource resource = new Resource();
resource.setDirectory(outputDirectory);
project.addResource(resource);
}
project.getProperties().setProperty(requirementsProperty, requirements.toString());
project.getProperties().setProperty(capabilitiesProperty, capabilities.toString());
if (activators.size() == 1) {
project.getProperties().setProperty(activatorProperty, activators.get(0).getName());
}
project.getProperties().setProperty("BNDExtension-Private-Package", "org.apache.karaf.util.tracker");
project.getProperties().setProperty("BNDPrependExtension-Import-Package", "!org.apache.karaf.util.tracker.annotation");
List<Class<?>> services = finder.findAnnotatedClasses(Service.class);
Set<String> packages = new TreeSet<>();
for (Class<?> clazz : services) {
packages.add(clazz.getPackage().getName());
}
if (!packages.isEmpty()) {
project.getProperties().setProperty("BNDExtension-Karaf-Commands", join(packages, ","));
}
} catch (Exception e) {
throw new MojoExecutionException("Error building commands help", e);
}
}
use of org.apache.maven.model.Resource in project sling by apache.
the class JcrOcmMojo method execute.
public void execute() throws MojoFailureException {
boolean hasFailures = false;
// prepare QDox and prime with the compile class path of the project
JavaDocBuilder builder = new JavaDocBuilder();
try {
builder.getClassLibrary().addClassLoader(this.getCompileClassLoader());
} catch (IOException ioe) {
throw new MojoFailureException("Cannot prepare QDox");
}
// add the sources from the project
for (Iterator i = this.project.getCompileSourceRoots().iterator(); i.hasNext(); ) {
try {
builder.addSourceTree(new File((String) i.next()));
} catch (OutOfMemoryError oome) {
// this may be the case for big sources and not enough VM mem
// drop the builder to help GC now
builder = null;
// fail with some explanation
throw new MojoFailureException("Failed analyzing source due to not enough memory, try setting Max Heap Size higher, e.g. using MAVEN_OPTS=-Xmx128m");
}
}
// parse the sources and get them
JavaSource[] javaSources = builder.getSources();
List descriptors = new ArrayList();
for (int i = 0; i < javaSources.length; i++) {
JavaClass[] javaClasses = javaSources[i].getClasses();
for (int j = 0; javaClasses != null && j < javaClasses.length; j++) {
DocletTag tag = javaClasses[j].getTagByName(ClassDescriptor.TAG_CLASS_DESCRIPTOR);
if (tag != null) {
ClassDescriptor descriptor = this.createClassDescriptor(javaClasses[j]);
if (descriptor != null) {
descriptors.add(descriptor);
} else {
hasFailures = true;
}
}
}
}
// after checking all classes, throw if there were any failures
if (hasFailures) {
throw new MojoFailureException("Jackrabbit OCM Descriptor parsing had failures (see log)");
}
// terminate if there is nothing to write
if (descriptors.isEmpty()) {
this.getLog().info("No Jackrabbit OCM Descriptors found in project");
return;
}
// finally the descriptors have to be written ....
if (StringUtils.isEmpty(this.finalName)) {
this.getLog().error("Descriptor file name must not be empty");
return;
}
// prepare the descriptor output file
File descriptorFile = new File(new File(this.outputDirectory, "SLING-INF"), this.finalName);
// ensure parent dir
descriptorFile.getParentFile().mkdirs();
this.getLog().info("Generating " + descriptors.size() + " OCM Mapping Descriptors to " + descriptorFile);
// write out all the class descriptors in parse order
FileOutputStream descriptorStream = null;
XMLWriter xw = null;
try {
descriptorStream = new FileOutputStream(descriptorFile);
xw = new XMLWriter(descriptorStream, false);
xw.printElementStart("jackrabbit-ocm", false);
for (Iterator di = descriptors.iterator(); di.hasNext(); ) {
ClassDescriptor sd = (ClassDescriptor) di.next();
sd.generate(xw);
}
xw.printElementEnd("jackrabbit-ocm");
} catch (IOException ioe) {
hasFailures = true;
this.getLog().error("Cannot write descriptor to " + descriptorFile, ioe);
throw new MojoFailureException("Failed to write descriptor to " + descriptorFile);
} finally {
IOUtil.close(xw);
IOUtil.close(descriptorStream);
// remove the descriptor file in case of write failure
if (hasFailures) {
descriptorFile.delete();
}
}
// now add the descriptor file to the maven resources
final String ourRsrcPath = this.outputDirectory.getAbsolutePath();
boolean found = false;
final Iterator rsrcIterator = this.project.getResources().iterator();
while (!found && rsrcIterator.hasNext()) {
final Resource rsrc = (Resource) rsrcIterator.next();
found = rsrc.getDirectory().equals(ourRsrcPath);
}
if (!found) {
final Resource resource = new Resource();
resource.setDirectory(this.outputDirectory.getAbsolutePath());
this.project.addResource(resource);
}
// and set include accordingly
this.project.getProperties().setProperty("Sling-Mappings", "SLING-INF/" + this.finalName);
}
use of org.apache.maven.model.Resource in project sling by apache.
the class SlingInitialContentMounter method mount.
/**
* Add configurations to a running OSGi instance for initial content.
* @param targetUrl The web console base url
* @param bundleFile The artifact (bundle)
* @throws MojoExecutionException
*/
public void mount(final String targetUrl, final File bundleFile) throws MojoExecutionException {
// first, let's get the manifest and see if initial content is configured
ManifestHeader header = null;
try {
final Manifest mf = getManifest(bundleFile);
final String value = mf.getMainAttributes().getValue(HEADER_INITIAL_CONTENT);
if (value == null) {
log.debug("Bundle has no initial content - no file system provider config created.");
return;
}
header = ManifestHeader.parse(value);
if (header == null || header.getEntries().length == 0) {
log.warn("Unable to parse header or header is empty: " + value);
return;
}
} catch (IOException ioe) {
throw new MojoExecutionException("Unable to read manifest from file " + bundleFile, ioe);
}
log.info("Trying to configure file system provider...");
// quick check if resources are configured
final List resources = project.getResources();
if (resources == null || resources.size() == 0) {
throw new MojoExecutionException("No resources configured for this project.");
}
final List<FsResourceConfiguration> cfgs = new ArrayList<>();
final Entry[] entries = header.getEntries();
for (final Entry entry : entries) {
String path = entry.getValue();
if (path != null && !path.endsWith("/")) {
path += "/";
}
// check if we should ignore this
final String ignoreValue = entry.getDirectiveValue("maven:mount");
if (ignoreValue != null && ignoreValue.equalsIgnoreCase("false")) {
log.debug("Ignoring " + path);
continue;
}
String installPath = entry.getDirectiveValue("path");
if (installPath == null) {
installPath = "/";
}
// search the path in the resources (usually this should be the first resource
// entry but this might be reconfigured
File dir = null;
final Iterator i = resources.iterator();
while (dir == null && i.hasNext()) {
final Resource rsrc = (Resource) i.next();
String child = path;
// if resource mapping defines a target path: remove target path from checked resource path
String targetPath = rsrc.getTargetPath();
if (targetPath != null && !targetPath.endsWith("/")) {
targetPath = targetPath + "/";
}
if (targetPath != null && path.startsWith(targetPath)) {
child = child.substring(targetPath.length());
}
dir = new File(rsrc.getDirectory(), child);
if (!dir.exists()) {
dir = null;
}
}
if (dir == null) {
throw new MojoExecutionException("No resource entry found containing " + path);
}
// check for root mapping - which we don't support atm
if ("/".equals(installPath)) {
throw new MojoExecutionException("Mapping to root path not supported by fs provider at the moment. Please adapt your initial content configuration.");
}
// check further initial content directives
StringBuilder importOptions = new StringBuilder();
String overwriteValue = entry.getDirectiveValue("overwrite");
if (StringUtils.isNotBlank(overwriteValue)) {
importOptions.append("overwrite:=" + overwriteValue);
}
String ignoreImportProvidersValue = entry.getDirectiveValue("ignoreImportProviders");
if (StringUtils.isNotBlank(overwriteValue)) {
if (importOptions.length() > 0) {
importOptions.append(";");
}
importOptions.append("ignoreImportProviders:=\"" + ignoreImportProvidersValue + "\"");
}
cfgs.add(new FsResourceConfiguration().fsMode(FsMode.INITIAL_CONTENT).contentRootDir(dir.getAbsolutePath()).providerRootPath(installPath).initialContentImportOptions(importOptions.toString()));
}
if (!cfgs.isEmpty()) {
helper.addConfigurations(targetUrl, cfgs);
}
}
Aggregations