public List<File> toFileList( FileSet fs ) throws MojoExecutionException { try { if ( fs.getDirectory() != null ) { File directory = new File( fs.getDirectory() ); String includes = toString( fs.getIncludes() ); String excludes = toString( fs.getExcludes() ); return FileUtils.getFiles( directory, includes, excludes ); } else { getLog().warn( String.format( "Fileset [%s] directory empty", fs.toString() ) ); return new ArrayList<>(); } } catch ( IOException e ) { throw new MojoExecutionException( String.format( "Unable to get paths to fileset [%s]", fs.toString() ), e ); } }
protected List<File> getFilesToProcess() throws MojoExecutionException { List<File> files = new ArrayList<>(); if ( null != getFileset() ) { if ( null == getFilesets() ) { filesets = new ArrayList<>(); } getFilesets().add( getFileset() ); } if ( null != getFilesets() ) { for ( FileSet fs : getFilesets() ) { if ( (null != fs) && (null != fs.getDirectory()) ) { FileSetTransformer fileMgr = new FileSetTransformer( fs ); files.addAll( fileMgr.toFileList() ); } } } return files; }
private void createIvyArchive(File sourceDir, File targetIar) throws MojoExecutionException { ZipArchiver archiver = new ZipArchiver(); archiver.setDestFile(targetIar); archiver.addFileSet(getDefaultFileset(sourceDir)); FileSetConverter fsConverter = new FileSetConverter(project.getBasedir()); for(org.codehaus.plexus.archiver.FileSet fs : fsConverter.toPlexusFileSets(iarFileSets)) { archiver.addFileSet(fs); } try { archiver.createArchive(); } catch (ArchiverException | IOException ex) { throw new MojoExecutionException("Failed to create IAR: " + targetIar.getAbsolutePath(), ex); } }
@Test public void canDefineCustomInclusions() throws Exception { IarPackagingMojo mojo = rule.getMojo(); File outputDir = new File(mojo.project.getBasedir(), "target"); File customPomXml = new File(outputDir, "myCustomPom.xml"); FileUtils.write(customPomXml, "customPomContent"); String relativeCustomIncludePath = "target/"+customPomXml.getName(); FileSet fs = new FileSet(); fs.setIncludes(Arrays.asList(relativeCustomIncludePath)); mojo.iarFileSets = new FileSet[]{fs}; mojo.execute(); try(ZipFile archive = new ZipFile(mojo.project.getArtifact().getFile())) { assertThat(archive.getEntry(relativeCustomIncludePath)).as("Custom inclusions must be included").isNotNull(); } }
@Test() public void testEmptyXmlFileSets() throws Exception { final File testPom = new File("src/test/resources/net/trajano/mojo/cleanpom/cleaner-pom.xml"); final File xml = new File("src/test/resources/net/trajano/mojo/cleanpom/dual-doctype.xml"); final File temp = File.createTempFile("dirty", ""); temp.delete(); temp.mkdirs(); FileUtils.copyFile(xml, new File(temp, "dirty1.xml")); final CleanXmlMojo mojo = (CleanXmlMojo) rule.lookupMojo("clean-xml", testPom); mojo.setXmlFileSets(new FileSet[0]); assertNotNull(mojo); try { mojo.execute(); } finally { FileUtils.deleteDirectory(temp); } }
@Test(expected = MojoExecutionException.class) public void testFailWithWithDTDs() throws Exception { final File testPom = new File("src/test/resources/net/trajano/mojo/cleanpom/cleaner-pom.xml"); final File xml = new File("src/test/resources/net/trajano/mojo/cleanpom/dual-doctype.xml"); final File temp = File.createTempFile("dirty", ""); temp.delete(); temp.mkdirs(); FileUtils.copyFile(xml, new File(temp, "dirty1.xml")); final CleanXmlMojo mojo = (CleanXmlMojo) rule.lookupMojo("clean-xml", testPom); final FileSet xmlFiles = new FileSet(); xmlFiles.setDirectory(temp.getAbsolutePath()); xmlFiles.addInclude("**/*.xml"); rule.setVariableValueToObject(mojo, "xmlFileSets", new FileSet[] { xmlFiles }); assertNotNull(mojo); try { mojo.execute(); } finally { FileUtils.deleteDirectory(temp); } }
Collection<String> getResourceFiles(final Iterable<? extends FileSet> fileSets, final boolean followLinks, final boolean allowDuplicates, final boolean allowFiles, final boolean allowDirs) throws MojoExecutionException { final Set<String> result = new LinkedHashSet<>(); for (final FileSet fileSet : Objects.requireNonNull(fileSets)) { final Collection<String> files = getResourceFiles(fileSet, followLinks, allowDuplicates, allowFiles, allowDirs); for (final String file : files) { if (!result.add(file) && !allowDuplicates) { getLog() .error( String.format("a duplicate file %s under directory %s", file, fileSet.getDirectory())); throw new MojoExecutionException(String.format("a duplicate file %s under directory %s", file, fileSet.getDirectory())); } } } return result; }
private Collection<String> getResourceFiles(final FileSet fileSet, final boolean followLinks, final boolean allowDuplicates, final boolean allowFiles, final boolean allowDirs) throws MojoExecutionException { final Path dirPath = Paths.get(Objects.requireNonNull(fileSet).getDirectory()); final FileSetPathMatcher matcher = new FileSetPathMatcher(fileSet.getIncludes(), fileSet.getExcludes(), dirPath); final EnumSet<FileVisitOption> options = followLinks ? EnumSet.of(FileVisitOption.FOLLOW_LINKS) : EnumSet .noneOf(FileVisitOption.class); final Set<String> result = new LinkedHashSet<>(); try { final ResourceVisitor resourceVisitor = new ResourceVisitor(matcher, allowDuplicates, allowFiles, allowDirs, result, getLog()); Files.walkFileTree(dirPath, options, Integer.MAX_VALUE, resourceVisitor); if (resourceVisitor.getErrorMessage() != null) { throw new MojoExecutionException(resourceVisitor.getErrorMessage()); } } catch (final IOException e) { getLog().error("fileSet caused error", e); throw new MojoExecutionException("fileSet caused error", e); } return result; }
/** * Method updateFileSet * * @param value * @param element * @param counter * @param xmlTag */ protected void updateFileSet( FileSet value, String xmlTag, Counter counter, Element element ) { boolean shouldExist = value != null; Element root = updateElement( counter, element, xmlTag, shouldExist ); if ( shouldExist ) { Counter innerCount = new Counter( counter.getDepth() + 1 ); findAndReplaceSimpleElement( innerCount, root, "directory", value.getDirectory(), null ); findAndReplaceSimpleLists( innerCount, root, value.getIncludes(), "includes", "include" ); findAndReplaceSimpleLists( innerCount, root, value.getExcludes(), "excludes", "exclude" ); } }
private static List<File> toFileList(FileSet fileSet) throws IOException { File directory = new File(fileSet.getDirectory()); if (directory.exists()) { String includes = toString(fileSet.getIncludes()); String excludes = toString(fileSet.getExcludes()); return FileUtils.getFiles(directory, includes, excludes); } return Collections.<File>emptyList(); }
private Set<String> getTmlFiles() throws IOException { Set<String> files = new HashSet<>(); for (FileSet fileSet : tmls) { File directory = (fileSet.getDirectory() == null) ? this.project.getBasedir() : new File(fileSet.getDirectory()); String includes = StringUtils.join(fileSet.getIncludes(), ','); String excludes = StringUtils.join(fileSet.getExcludes(), ','); for (File file : FileUtils.getFiles(directory, includes, excludes)) { files.add(file.getAbsolutePath()); } } return files; }
/** * This method retrieves a list of ".aliaslib" files to process in the * source directory (usually "target/src") which is a copy of the actual * source directory used to compile the TIBCO BusinessWorks EAR. */ private List<File> initFiles() throws IOException { FileSet restriction = new FileSet(); File directory = buildSrcDirectory; if (directory == null) { directory = new File("."); getLog().warn(SRC_NOT_SET); } getLog().debug(directory.getAbsolutePath()); restriction.setDirectory(directory.getAbsolutePath()); restriction.addInclude("**/*.aliaslib"); List<File> result = AbstractProjectsListMojo.toFileList(restriction); if (customAliasLibDirectories != null && !customAliasLibDirectories.isEmpty()) { for (File customDirectory : customAliasLibDirectories) { getLog().debug("Looking for '.aliaslib' files in custom directory: " + customDirectory); FileSet customRestriction = new FileSet(); customRestriction.setDirectory(customDirectory.getAbsolutePath()); customRestriction.addInclude("**/*.aliaslib"); result.addAll(AbstractProjectsListMojo.toFileList(customRestriction)); } } getLog().debug("List of '.aliaslib' files to update: " + result); return result; }
protected final void initProjects(String mandatoryFilename, HashMap<String, String> m, Class<? extends AbstractProject> classAbstractProject) throws IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException, XmlPullParserException { projects = new ArrayList<AbstractProject>(); restriction= new FileSet(); restriction.setDirectory(workspaceRoot.getAbsolutePath()); if (recursive) { for (String p : patterns) { restriction.addInclude(p + mandatoryFilename); } } else { restriction.addInclude(mandatoryFilename); // we are only looking in the "workspaceRoot" directory } restriction.addExclude("**/target/**/" + mandatoryFilename); restriction.addExclude("**/bin/" + mandatoryFilename); List<File> files = toFileList(restriction); for (File f : files) { Constructor<? extends AbstractProject> ctor = classAbstractProject.getConstructor(); AbstractProject ap = ctor.newInstance(); ap.initialize(f, workspaceRoot, m, mandatoryFilename, getLog()); if (!ap.isIgnored()) { projects.add(ap); } else { getLog().debug("Ignoring project '" + ap.getProjectName() + "'"); } } }
protected List<File> initFiles() throws IOException { getLog().debug("Looking for files to process..."); FileSet restriction = new FileSet(); File directory = getDirectoryToProcess(); if (directory == null) { directory = new File("."); } restriction.setDirectory(directory.getAbsolutePath()); restriction.addInclude("**/*.process"); return AbstractProjectsListMojo.toFileList(restriction); }
@Override protected List<File> initFiles() throws IOException { getLog().debug("Looking for files to process..."); FileSet restriction = new FileSet(); restriction.setDirectory(getDirectoryToProcess().getAbsolutePath()); restriction.addInclude("**/*.xml"); return AbstractProjectsListMojo.toFileList(restriction); }
/** * A {@link DirectoryScanner} boiler plate. * * @param fileSet {@link FileSet} to scan * @return the included paths */ private String[] scan( FileSet fileSet ) { File basedir = new File( fileSet.getDirectory() ); if ( !basedir.exists() || !basedir.isDirectory() ) { return null; } DirectoryScanner scanner = new DirectoryScanner(); List<String> includes = fileSet.getIncludes(); List<String> excludes = fileSet.getExcludes(); if ( includes != null && includes.size() > 0 ) { scanner.setIncludes( includes.toArray( new String[0] ) ); } if ( excludes != null && excludes.size() > 0 ) { scanner.setExcludes( excludes.toArray( new String[0] ) ); } scanner.setBasedir( basedir ); scanner.scan(); return scanner.getIncludedFiles(); }
public static List<File> cqlFiles(FileSet fileSet) throws IOException { File directory = new File(fileSet.getDirectory()); String includes = fileSet.getIncludes().stream().collect(joining(", ")); String excludes = fileSet.getExcludes().stream().collect(joining(", ")); @SuppressWarnings("unchecked") List<File> files = FileUtils.getFiles(directory, includes, excludes); Collections.sort(files); return files; }
/** * Performs the conversion. * * @throws MojoExecutionException * thrown when there is a problem executing Mjo. */ @Override public void execute() throws MojoExecutionException { destDir.mkdirs(); if (fontFileSets == null) { final FileSet defaultFontFileSet = new FileSet(); defaultFontFileSet.setDirectory(new File(project.getBasedir(), "src/main/ttf").getPath()); defaultFontFileSet.addInclude("**/*.ttf"); fontFileSets = Collections.singletonList(defaultFontFileSet); } for (final FileSet fileSet : fontFileSets) { final String directory = fileSet.getDirectory(); final File baseDirectory = new File(directory); // NOPMD if (!baseDirectory.isDirectory()) { // NOPMD getLog().warn(format(R.getString("missingdir"), directory)); continue; } final Scanner scanner = buildContext.newScanner(baseDirectory); scanner.setIncludes(fileSet.getIncludes().toArray(new String[0])); // NOPMD scanner.setExcludes(fileSet.getExcludes().toArray(new String[0])); // NOPMD scanner.scan(); for (final String includedFile : scanner.getIncludedFiles()) { final File inputFile = new File(baseDirectory, // NOPMD includedFile); final String basename = includedFile.substring(0, includedFile.lastIndexOf('.')); final File svgFile = new File(destDir, basename + ".svg"); // NOPMD try { final PrintStream ps = new PrintStream(// NOPMD buildContext.newFileOutputStream(svgFile)); SvgFontUtil.writeFontAsSvg(ps, Font.create(inputFile.toString()), basename); ps.close(); } catch (final Exception e) { throw new MojoExecutionException(String.format(R.getString("failedtorender"), inputFile), e); } } } }
protected List<URI> createResourceEntryUris(ResourceEntry resourceEntry, String defaultDirectory, String[] defaultIncludes, String[] defaultExcludes) throws MojoExecutionException { if (resourceEntry == null) { return Collections.emptyList(); } else { final List<URI> uris = new LinkedList<URI>(); if (resourceEntry.getFileset() != null) { final FileSet fileset = resourceEntry.getFileset(); uris.addAll(createFileSetUris(fileset, defaultDirectory, defaultIncludes, defaultExcludes)); } if (resourceEntry.getUrl() != null) { String urlDraft = resourceEntry.getUrl(); uris.add(createUri(urlDraft)); } if (resourceEntry.getDependencyResource() != null) { final String systemId = resourceEntry.getDependencyResource() .getSystemId(); try { URI uri = new URI(systemId); uris.add(uri); } catch (URISyntaxException e) { throw new MojoExecutionException( MessageFormat.format( "Could not create the resource entry URI from the following system id: [{0}].", systemId), e); } } return uris; } }
@Test public void testWithDTDAndPI() throws Exception { final File testPom = new File("src/test/resources/net/trajano/mojo/cleanpom/cleaner-pom.xml"); final File xml = new File("src/test/resources/net/trajano/mojo/cleanpom/checkstyle-configuration-with-pi.xml"); final File temp = File.createTempFile("dirty", ""); temp.delete(); temp.mkdirs(); FileUtils.copyFile(xml, new File(temp, "dirty1.xml")); final CleanXmlMojo mojo = (CleanXmlMojo) rule.lookupMojo("clean-xml", testPom); final FileSet xmlFiles = new FileSet(); xmlFiles.setDirectory(temp.getAbsolutePath()); xmlFiles.addInclude("**/*.xml"); rule.setVariableValueToObject(mojo, "xmlFileSets", new FileSet[] { xmlFiles }); assertNotNull(mojo); try { mojo.execute(); final FileInputStream fileInputStream = new FileInputStream(new File(temp, "dirty1.xml")); final String data = IOUtils.toString(fileInputStream); assertTrue(data.length() > 10); fileInputStream.close(); } finally { FileUtils.deleteDirectory(temp); } }
@Test public void testWithPI() throws Exception { final File testPom = new File("src/test/resources/net/trajano/mojo/cleanpom/cleaner-pom.xml"); final File xml = new File("src/test/resources/net/trajano/mojo/cleanpom/dirty-with-pi.xml"); final File temp = File.createTempFile("dirty", ""); temp.delete(); temp.mkdirs(); FileUtils.copyFile(xml, new File(temp, "dirty1.xml")); final CleanXmlMojo mojo = (CleanXmlMojo) rule.lookupMojo("clean-xml", testPom); final FileSet xmlFiles = new FileSet(); xmlFiles.setDirectory(temp.getAbsolutePath()); xmlFiles.addInclude("**/*.xml"); rule.setVariableValueToObject(mojo, "xmlFileSets", new FileSet[] { xmlFiles }); assertNotNull(mojo); try { mojo.execute(); final FileInputStream fileInputStream = new FileInputStream(new File(temp, "dirty1.xml")); final String data = IOUtils.toString(fileInputStream); fileInputStream.close(); assertTrue(data.contains("<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>")); } finally { FileUtils.deleteDirectory(temp); } }
private List<FileSet> getFileSetsWithNonExistentIncludes(final Path rootDir) throws IOException { final List<FileSet> fileSets = getFileSets(rootDir); final FileSet fileSet = new FileSet(); fileSet.setDirectory(rootDir.toString()); for (final String arg : BAD_ARGS) { fileSet.addInclude(arg); } fileSets.add(fileSet); return fileSets; }
private List<FileSet> getFileSetsWithDuplicates(final Path rootDir) throws IOException { final List<FileSet> fileSets = getFileSets(rootDir); final FileSet fileSet = new FileSet(); fileSet.setDirectory(rootDir.toString()); fileSet.addInclude("**/file4"); fileSets.add(fileSet); return fileSets; }
protected void mergeFileSet_Directory( FileSet target, FileSet source, boolean sourceDominant, Map<Object, Object> context ) { String src = source.getDirectory(); if ( src != null ) { if ( sourceDominant || target.getDirectory() == null ) { target.setDirectory( src ); target.setLocation( "directory", source.getLocation( "directory" ) ); } } }
private FileSetTransformer( FileSet fileSet ) { this.fileSet = fileSet; }
/** * @return the filesets */ public List<FileSet> getFilesets() { return filesets; }
/** * @return the fileset */ public FileSet getFileset() { return fileset; }
public static List<File> toFileList(FileSet fileSet) throws IOException { File directory = new File(fileSet.getDirectory()); String includes = toCommaSeparatedString(fileSet.getIncludes()); String excludes = toCommaSeparatedString(fileSet.getExcludes()); return FileUtils.getFiles(directory, includes, excludes); }
public FileSet getFileset() { return fileset; }
public void setFileset(FileSet fileset) { this.fileset = fileset; }
private List<URI> createFileSetUris(final FileSet fileset, String defaultDirectory, String[] defaultIncludes, String defaultExcludes[]) throws MojoExecutionException { final String draftDirectory = fileset.getDirectory(); final String directory = draftDirectory == null ? defaultDirectory : draftDirectory; final List<String> includes; @SuppressWarnings("unchecked") final List<String> draftIncludes = (List<String>) fileset.getIncludes(); if (draftIncludes == null || draftIncludes.isEmpty()) { includes = defaultIncludes == null ? Collections .<String> emptyList() : Arrays.asList(defaultIncludes); } else { includes = draftIncludes; } final List<String> excludes; @SuppressWarnings("unchecked") final List<String> draftExcludes = (List<String>) fileset.getExcludes(); if (draftExcludes == null || draftExcludes.isEmpty()) { excludes = defaultExcludes == null ? Collections .<String> emptyList() : Arrays.asList(defaultExcludes); } else { excludes = draftExcludes; } String[] includesArray = includes.toArray(new String[includes.size()]); String[] excludesArray = excludes.toArray(new String[excludes.size()]); try { final List<File> files = IOUtils.scanDirectoryForFiles( getBuildContext(), new File(directory), includesArray, excludesArray, !getDisableDefaultExcludes()); final List<URI> uris = new ArrayList<URI>(files.size()); for (final File file : files) { // try { final URI uri = file.toURI(); uris.add(uri); // } catch (MalformedURLException murlex) { // throw new MojoExecutionException( // MessageFormat.format( // "Could not create an URL for the file [{0}].", // file), murlex); // } } return uris; } catch (IOException ioex) { throw new MojoExecutionException( MessageFormat .format("Could not scan directory [{0}] for files with inclusion [{1}] and exclusion [{2}].", directory, includes, excludes)); } }
public List<FileSet> getFileSets() { return _fileSets; }
public void setFileSets(final List<? extends FileSet> fileSets) { _fileSets = new ArrayList<>(fileSets); }
@Before public void setUp() throws Exception { // given final Properties properties = new Properties(); try (InputStream in = getClass().getResourceAsStream("test.properties")) { properties.load(in); } execLocation = properties.getProperty("plugin.test.exec"); assertThat(execLocation).isIn("rm", "del"); // errorFile = temp.newFile("errors.txt"); errorFile = new File(temp.getRoot(), "errors.txt"); // outFile = temp.newFile("result"); outFile = new File(temp.getRoot(), "result"); workDir = temp.newFolder(); expectedFiles = makeTestTree(workDir.toPath(), SUBDIRS, SUBFILES); rootDir = workDir.toPath(); glob = GLOB_ALL; args = new ArrayList<>(); mojo = new ExecMojo(component); mojo.setExecLocation(execLocation); mojo.setExecLocationAsIs(true); mojo.setSystemCommand(true); mojo.setArgQuote("\""); mojo.setEnvironment(environment); mojo.setErrorFile(errorFile); mojo.setRedirectErrorStream(false); mojo.setErrorInherit(false); mojo.setErrorPipe(true); mojo.setErrorProperty(""); mojo.setErrorAppend(false); mojo.setOutAppend(false); mojo.setOutFile(outFile); mojo.setOutInherit(false); mojo.setOutPipe(true); mojo.setOutProperty(""); mojo.setWorkDir(workDir); mojo.setAllowDuplicates(true); mojo.setAllowFiles(true); mojo.setAllowDirs(false); mojo.setFileSets(Collections.<FileSet>emptyList()); }
protected void mergeFileSet( FileSet target, FileSet source, boolean sourceDominant, Map<Object, Object> context ) { mergePatternSet( target, source, sourceDominant, context ); mergeFileSet_Directory( target, source, sourceDominant, context ); }
/** * Converts a Maven FileSet to a list of File objects. * * @param source FileSet to convert * @return List containing every element of the FileSet as a File * @throws IOException if an I/O error occurs while trying to find the files */ @SuppressWarnings("unchecked") public static List<File> convertFileSetToFiles(FileSet source) throws IOException { String includes = getCommaSeparatedList(source.getIncludes()); String excludes = getCommaSeparatedList(source.getExcludes()); return FileUtils.getFiles(new File(source.getDirectory()), includes, excludes); }
/** * Converts a Maven FileSet to a list of File objects. * * @param source FileSet to convert * @return List<File> containing every element of the FileSet as a File * @throws IOException if an I/O error occurs while trying to find the files */ @SuppressWarnings("unchecked") public static List<File> convertFileSetToFiles(FileSet source) throws IOException { String includes = getCommaSeparatedList(source.getIncludes()); String excludes = getCommaSeparatedList(source.getExcludes()); return FileUtils.getFiles(new File(source.getDirectory()), includes, excludes); }