Search in sources :

Example 1 with FileSet

use of org.apache.maven.archetype.metadata.FileSet in project maven-archetype by apache.

the class DefaultFilesetArchetypeGenerator method processTemplates.

private void processTemplates(String packageName, File outputDirectoryFile, Context context, AbstractArchetypeDescriptor archetypeDescriptor, List<String> archetypeResources, ZipFile archetypeZipFile, String moduleOffset, boolean failIfExists) throws OutputFileExists, ArchetypeGenerationFailure, FileNotFoundException, IOException {
    Iterator<FileSet> iterator = archetypeDescriptor.getFileSets().iterator();
    if (iterator.hasNext()) {
        getLogger().debug("Processing filesets" + "\n  " + archetypeResources);
    }
    int count = 0;
    while (iterator.hasNext()) {
        FileSet fileSet = iterator.next();
        count++;
        List<String> fileSetResources = archetypeFilesResolver.filterFiles(moduleOffset, fileSet, archetypeResources);
        // This creates an empty directory, even if there is no file to process
        // Fix for ARCHETYPE-57
        getOutputFile(moduleOffset, fileSet.getDirectory(), outputDirectoryFile, fileSet.isPackaged(), packageName, moduleOffset, context).mkdirs();
        if (fileSet.isFiltered()) {
            getLogger().debug("    Processing fileset " + fileSet + " -> " + fileSetResources.size() + ":\n      " + fileSetResources);
            int processed = processFileSet(fileSet.getDirectory(), fileSetResources, fileSet.isPackaged(), packageName, context, outputDirectoryFile, moduleOffset, getEncoding(fileSet.getEncoding()), failIfExists);
            getLogger().debug("    Processed " + processed + " files.");
        } else {
            getLogger().debug("    Copying fileset " + fileSet + " -> " + fileSetResources.size() + ":\n      " + fileSetResources);
            int copied = copyFiles(fileSet.getDirectory(), fileSetResources, fileSet.isPackaged(), packageName, outputDirectoryFile, archetypeZipFile, moduleOffset, failIfExists, context);
            getLogger().debug("    Copied " + copied + " files.");
        }
    }
    getLogger().debug("Processed " + count + " filesets");
}
Also used : FileSet(org.apache.maven.archetype.metadata.FileSet)

Example 2 with FileSet

use of org.apache.maven.archetype.metadata.FileSet in project maven-archetype by apache.

the class TestDefaultArchetypeFilesResolver method testResourceFiltering.

public void testResourceFiltering() throws Exception {
    FileSet fileSet = new FileSet();
    fileSet.addInclude("**/*.java");
    fileSet.setDirectory("src/main/java");
    fileSet.setEncoding("UTF-8");
    fileSet.setPackaged(true);
    fileSet.setFiltered(true);
    List<String> archetypeResources = new ArrayList<String>();
    archetypeResources.add("pom.xml");
    archetypeResources.add("App.java");
    archetypeResources.add("src/main/c/App.c");
    archetypeResources.add("src/main/java/App.java");
    archetypeResources.add("src/main/java/inner/package/App2.java");
    archetypeResources.add("src/main/mdo/App.mdo");
    archetypeResources.add("src/main/resources/App.properties");
    archetypeResources.add("src/main/resources/inner/dir/App2.properties");
    archetypeResources.add("src/test/c/AppTest.c");
    archetypeResources.add("src/test/java/AppTest.java");
    archetypeResources.add("src/test/mdo/AppTest.mdo");
    archetypeResources.add("src/test/resources/AppTest.properties");
    System.out.println("FileSet:" + fileSet);
    System.out.println("Resources:" + archetypeResources);
    ArchetypeFilesResolver resolver = new DefaultArchetypeFilesResolver();
    List<String> fileSetResources = resolver.filterFiles("", fileSet, archetypeResources);
    System.out.println("Result:" + fileSetResources);
    assertEquals(2, fileSetResources.size());
}
Also used : FileSet(org.apache.maven.archetype.metadata.FileSet) ArrayList(java.util.ArrayList)

Example 3 with FileSet

use of org.apache.maven.archetype.metadata.FileSet in project maven-archetype by apache.

the class FilesetArchetypeCreator method createModule.

private ModuleDescriptor createModule(Properties reverseProperties, String rootArtifactId, String moduleId, String packageName, File basedir, File archetypeFilesDirectory, List<String> languages, List<String> filtereds, String defaultEncoding, boolean preserveCData, boolean keepParent) throws IOException, XmlPullParserException {
    ModuleDescriptor archetypeDescriptor = new ModuleDescriptor();
    getLogger().debug("Starting module's descriptor " + moduleId);
    archetypeFilesDirectory.mkdirs();
    getLogger().debug("Module's files output directory " + archetypeFilesDirectory);
    Model pom = pomManager.readPom(FileUtils.resolveFile(basedir, Constants.ARCHETYPE_POM));
    String replacementId = pom.getArtifactId();
    String moduleDirectory = pom.getArtifactId();
    if (replacementId.indexOf(rootArtifactId) >= 0) {
        replacementId = StringUtils.replace(replacementId, rootArtifactId, "${rootArtifactId}");
        moduleDirectory = StringUtils.replace(moduleId, rootArtifactId, "__rootArtifactId__");
    }
    if (moduleId.indexOf(rootArtifactId) >= 0) {
        moduleDirectory = StringUtils.replace(moduleId, rootArtifactId, "__rootArtifactId__");
    }
    archetypeDescriptor.setName(replacementId);
    archetypeDescriptor.setId(replacementId);
    archetypeDescriptor.setDir(moduleDirectory);
    setArtifactId(reverseProperties, pom.getArtifactId());
    List<String> excludePatterns = reverseProperties.getProperty(Constants.EXCLUDE_PATTERNS) != null ? Arrays.asList(StringUtils.split(reverseProperties.getProperty(Constants.EXCLUDE_PATTERNS), ",")) : Collections.<String>emptyList();
    List<String> fileNames = resolveFileNames(pom, basedir, excludePatterns);
    List<FileSet> filesets = resolveFileSets(packageName, fileNames, languages, filtereds, defaultEncoding);
    getLogger().debug("Resolved filesets for module " + archetypeDescriptor.getName());
    archetypeDescriptor.setFileSets(filesets);
    createArchetypeFiles(reverseProperties, filesets, packageName, basedir, archetypeFilesDirectory, defaultEncoding, excludePatterns);
    getLogger().debug("Created files for module " + archetypeDescriptor.getName());
    String parentArtifactId = reverseProperties.getProperty(Constants.PARENT_ARTIFACT_ID);
    setParentArtifactId(reverseProperties, pom.getArtifactId());
    for (String subModuleId : pom.getModules()) {
        String subModuleIdDirectory = subModuleId;
        if (subModuleId.indexOf(rootArtifactId) >= 0) {
            subModuleIdDirectory = StringUtils.replace(subModuleId, rootArtifactId, "__rootArtifactId__");
        }
        getLogger().debug("Creating module " + subModuleId);
        ModuleDescriptor moduleDescriptor = createModule(reverseProperties, rootArtifactId, subModuleId, packageName, FileUtils.resolveFile(basedir, subModuleId), FileUtils.resolveFile(archetypeFilesDirectory, subModuleIdDirectory), languages, filtereds, defaultEncoding, preserveCData, keepParent);
        archetypeDescriptor.addModule(moduleDescriptor);
        getLogger().debug("Added module " + moduleDescriptor.getName() + " in " + archetypeDescriptor.getName());
    }
    restoreParentArtifactId(reverseProperties, parentArtifactId);
    restoreArtifactId(reverseProperties, pom.getArtifactId());
    getLogger().debug("Created Module " + archetypeDescriptor.getName() + " pom");
    return archetypeDescriptor;
}
Also used : ModuleDescriptor(org.apache.maven.archetype.metadata.ModuleDescriptor) FileSet(org.apache.maven.archetype.metadata.FileSet) Model(org.apache.maven.model.Model)

Example 4 with FileSet

use of org.apache.maven.archetype.metadata.FileSet in project maven-archetype by apache.

the class FilesetArchetypeCreator method createFileSet.

private FileSet createFileSet(final List<String> excludes, final boolean packaged, final boolean filtered, final String group, final List<String> includes, String defaultEncoding) {
    FileSet fileSet = new FileSet();
    fileSet.setDirectory(group);
    fileSet.setPackaged(packaged);
    fileSet.setFiltered(filtered);
    fileSet.setIncludes(includes);
    fileSet.setExcludes(excludes);
    fileSet.setEncoding(defaultEncoding);
    getLogger().debug("Created Fileset " + fileSet);
    return fileSet;
}
Also used : FileSet(org.apache.maven.archetype.metadata.FileSet)

Example 5 with FileSet

use of org.apache.maven.archetype.metadata.FileSet in project maven-archetype by apache.

the class FilesetArchetypeCreator method createArchetype.

public void createArchetype(ArchetypeCreationRequest request, ArchetypeCreationResult result) {
    MavenProject project = request.getProject();
    List<String> languages = request.getLanguages();
    List<String> filtereds = request.getFiltereds();
    String defaultEncoding = request.getDefaultEncoding();
    boolean preserveCData = request.isPreserveCData();
    boolean keepParent = request.isKeepParent();
    boolean partialArchetype = request.isPartialArchetype();
    File outputDirectory = request.getOutputDirectory();
    File basedir = project.getBasedir();
    Properties properties = new Properties();
    Properties configurationProperties = new Properties();
    if (request.getProperties() != null) {
        properties.putAll(request.getProperties());
        configurationProperties.putAll(request.getProperties());
    }
    extractPropertiesFromProject(project, properties, configurationProperties, request.getPackageName());
    if (outputDirectory == null) {
        getLogger().debug("No output directory defined, using default: " + DEFAULT_OUTPUT_DIRECTORY);
        outputDirectory = FileUtils.resolveFile(basedir, DEFAULT_OUTPUT_DIRECTORY);
    }
    outputDirectory.mkdirs();
    getLogger().debug("Creating archetype in " + outputDirectory);
    try {
        File archetypePomFile = createArchetypeProjectPom(project, request.getProjectBuildingRequest(), configurationProperties, outputDirectory);
        File archetypeResourcesDirectory = new File(outputDirectory, getTemplateOutputDirectory());
        File archetypeFilesDirectory = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_RESOURCES);
        getLogger().debug("Archetype's files output directory " + archetypeFilesDirectory);
        File archetypeDescriptorFile = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_DESCRIPTOR);
        archetypeDescriptorFile.getParentFile().mkdirs();
        File archetypePostGenerationScript = new File(archetypeResourcesDirectory, Constants.ARCHETYPE_POST_GENERATION_SCRIPT);
        archetypePostGenerationScript.getParentFile().mkdirs();
        if (request.getProject().getBuild() != null && CollectionUtils.isNotEmpty(request.getProject().getBuild().getResources())) {
            for (Resource resource : request.getProject().getBuild().getResources()) {
                File inputFile = new File(resource.getDirectory() + File.separator + Constants.ARCHETYPE_POST_GENERATION_SCRIPT);
                if (inputFile.exists()) {
                    FileUtils.copyFile(inputFile, archetypePostGenerationScript);
                }
            }
        }
        getLogger().debug("Starting archetype's descriptor " + project.getArtifactId());
        ArchetypeDescriptor archetypeDescriptor = new ArchetypeDescriptor();
        archetypeDescriptor.setName(project.getArtifactId());
        archetypeDescriptor.setPartial(partialArchetype);
        addRequiredProperties(archetypeDescriptor, properties);
        // TODO ensure reverseProperties contains NO dotted properties
        Properties reverseProperties = getReversedProperties(archetypeDescriptor, properties);
        // reverseProperties.remove( Constants.GROUP_ID );
        // TODO ensure pomReversedProperties contains NO dotted properties
        Properties pomReversedProperties = getReversedProperties(archetypeDescriptor, properties);
        // pomReversedProperties.remove( Constants.PACKAGE );
        String packageName = configurationProperties.getProperty(Constants.PACKAGE);
        Model pom = pomManager.readPom(FileUtils.resolveFile(basedir, Constants.ARCHETYPE_POM));
        List<String> excludePatterns = configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS) != null ? Arrays.asList(StringUtils.split(configurationProperties.getProperty(Constants.EXCLUDE_PATTERNS), ",")) : Collections.<String>emptyList();
        List<String> fileNames = resolveFileNames(pom, basedir, excludePatterns);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Scanned for files " + fileNames.size());
            for (String name : fileNames) {
                getLogger().debug("- " + name);
            }
        }
        List<FileSet> filesets = resolveFileSets(packageName, fileNames, languages, filtereds, defaultEncoding);
        getLogger().debug("Resolved filesets for " + archetypeDescriptor.getName());
        archetypeDescriptor.setFileSets(filesets);
        createArchetypeFiles(reverseProperties, filesets, packageName, basedir, archetypeFilesDirectory, defaultEncoding, excludePatterns);
        getLogger().debug("Created files for " + archetypeDescriptor.getName());
        setParentArtifactId(reverseProperties, configurationProperties.getProperty(Constants.ARTIFACT_ID));
        for (String moduleId : pom.getModules()) {
            String rootArtifactId = configurationProperties.getProperty(Constants.ARTIFACT_ID);
            String moduleIdDirectory = moduleId;
            if (moduleId.indexOf(rootArtifactId) >= 0) {
                moduleIdDirectory = StringUtils.replace(moduleId, rootArtifactId, "__rootArtifactId__");
            }
            getLogger().debug("Creating module " + moduleId);
            ModuleDescriptor moduleDescriptor = createModule(reverseProperties, rootArtifactId, moduleId, packageName, FileUtils.resolveFile(basedir, moduleId), new File(archetypeFilesDirectory, moduleIdDirectory), languages, filtereds, defaultEncoding, preserveCData, keepParent);
            archetypeDescriptor.addModule(moduleDescriptor);
            getLogger().debug("Added module " + moduleDescriptor.getName() + " in " + archetypeDescriptor.getName());
        }
        restoreParentArtifactId(reverseProperties, null);
        restoreArtifactId(reverseProperties, configurationProperties.getProperty(Constants.ARTIFACT_ID));
        createPoms(pom, configurationProperties.getProperty(Constants.ARTIFACT_ID), configurationProperties.getProperty(Constants.ARTIFACT_ID), archetypeFilesDirectory, basedir, pomReversedProperties, preserveCData, keepParent);
        getLogger().debug("Created Archetype " + archetypeDescriptor.getName() + " template pom(s)");
        Writer out = null;
        try {
            out = WriterFactory.newXmlWriter(archetypeDescriptorFile);
            ArchetypeDescriptorXpp3Writer writer = new ArchetypeDescriptorXpp3Writer();
            writer.write(out, archetypeDescriptor);
            getLogger().debug("Archetype " + archetypeDescriptor.getName() + " descriptor written");
        } finally {
            IOUtil.close(out);
        }
        createArchetypeBasicIt(archetypeDescriptor, outputDirectory);
        // Copy archetype integration tests.
        File archetypeIntegrationTestInputFolder = new File(basedir, Constants.SRC + File.separator + "it" + File.separator + "projects");
        File archetypeIntegrationTestOutputFolder = new File(outputDirectory, Constants.SRC + File.separator + Constants.TEST + File.separator + Constants.RESOURCES + File.separator + "projects");
        if (archetypeIntegrationTestInputFolder.exists()) {
            getLogger().info("Copying: " + archetypeIntegrationTestInputFolder.getAbsolutePath() + " into " + archetypeIntegrationTestOutputFolder.getAbsolutePath());
            FileUtils.copyDirectoryStructure(archetypeIntegrationTestInputFolder, archetypeIntegrationTestOutputFolder);
        }
        InvocationRequest internalRequest = new DefaultInvocationRequest();
        internalRequest.setPomFile(archetypePomFile);
        internalRequest.setGoals(Collections.singletonList(request.getPostPhase()));
        if (request.getLocalRepository() != null) {
            internalRequest.setLocalRepositoryDirectory(new File(request.getLocalRepository().getBasedir()));
        }
        InvocationResult invokerResult = invoker.execute(internalRequest);
        if (invokerResult.getExitCode() != 0) {
            if (invokerResult.getExecutionException() != null) {
                throw invokerResult.getExecutionException();
            } else {
                throw new Exception("Invoker process ended with result different than 0!");
            }
        }
    } catch (Exception e) {
        result.setCause(e);
    }
}
Also used : FileSet(org.apache.maven.archetype.metadata.FileSet) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) ArchetypeDescriptor(org.apache.maven.archetype.metadata.ArchetypeDescriptor) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) TemplateCreationException(org.apache.maven.archetype.exception.TemplateCreationException) IOException(java.io.IOException) ArchetypeDescriptorXpp3Writer(org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Writer) ModuleDescriptor(org.apache.maven.archetype.metadata.ModuleDescriptor) MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File) ArchetypeDescriptorXpp3Writer(org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Writer) Writer(java.io.Writer)

Aggregations

FileSet (org.apache.maven.archetype.metadata.FileSet)6 ModuleDescriptor (org.apache.maven.archetype.metadata.ModuleDescriptor)2 Model (org.apache.maven.model.Model)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 TemplateCreationException (org.apache.maven.archetype.exception.TemplateCreationException)1 ArchetypeDescriptor (org.apache.maven.archetype.metadata.ArchetypeDescriptor)1 ArchetypeDescriptorXpp3Writer (org.apache.maven.archetype.metadata.io.xpp3.ArchetypeDescriptorXpp3Writer)1 Resource (org.apache.maven.model.Resource)1 MavenProject (org.apache.maven.project.MavenProject)1 DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)1 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)1 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)1 DirectoryScanner (org.codehaus.plexus.util.DirectoryScanner)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1