Java Code Examples for org.codehaus.plexus.util.FileUtils#forceDelete()

The following examples show how to use org.codehaus.plexus.util.FileUtils#forceDelete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RevertMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
public void execute()
    throws MojoExecutionException, MojoFailureException
{
    File outFile = project.getFile();
    File backupFile = new File( outFile.getParentFile(), outFile.getName() + ".versionsBackup" );

    if ( backupFile.exists() )
    {
        getLog().info( "Restoring " + outFile + " from " + backupFile );
        try
        {
            FileUtils.copyFile( backupFile, outFile );
            FileUtils.forceDelete( backupFile );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( e.getMessage(), e );
        }
    }
}
 
Example 2
Source File: CommitMojo.java    From versions-maven-plugin with Apache License 2.0 6 votes vote down vote up
public void execute()
    throws MojoExecutionException, MojoFailureException
{
    File outFile = project.getFile();
    File backupFile = new File( outFile.getParentFile(), outFile.getName() + ".versionsBackup" );

    if ( backupFile.exists() )
    {
        getLog().info( "Accepting all changes to " + outFile );
        try
        {
            FileUtils.forceDelete( backupFile );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( e.getMessage(), e );
        }
    }
}
 
Example 3
Source File: ThemeBuilderUtils.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Stores the given properties object in a file named <code>theme-derived.properties</code> within the
 * given theme directory
 *
 * @param themeDirectory directory the properties file should be created in
 * @param themeProperties properties that should be written to the properties file
 * @throws IOException
 */
public static void storeThemeProperties(String themeDirectory, Properties themeProperties) throws IOException {
    File propertiesFile = new File(themeDirectory, ThemeBuilderConstants.THEME_DERIVED_PROPERTIES_FILE);

    // need to remove file if already exists so the new properties will be written
    if (propertiesFile.exists()) {
        FileUtils.forceDelete(propertiesFile);
    }

    FileWriter fileWriter = null;

    try {
        fileWriter = new FileWriter(propertiesFile);

        themeProperties.store(fileWriter, null);
    } finally {
        if (fileWriter != null) {
            fileWriter.close();
        }
    }
}
 
Example 4
Source File: SignToolTest.java    From webstart with MIT License 5 votes vote down vote up
protected void copyTestFile( File file, File target )
    throws IOException
{
    if ( target.exists() )
    {
        FileUtils.forceDelete( target );
    }

    FileUtils.copyFile( file, target );

}
 
Example 5
Source File: DriverInstaller.java    From webdriverextensions-maven-plugin with Apache License 2.0 5 votes vote down vote up
private void moveAllFilesInDirectory(Path from, Path to) throws MojoExecutionException {
    try {
        Files.createDirectories(to);
        for (File file : from.toFile().listFiles()) {
            mojo.getLog().info("  Moving (All Files) " + file + " to " + to.resolve(file.toPath().getFileName()));
            FileUtils.forceDelete(to.resolve(file.toPath().getFileName()).toFile());
            Files.move(file.toPath(), to.resolve(file.toPath().getFileName()));
            makeExecutable(to.resolve(file.toPath().getFileName()));
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to move directory " + quote(from) + " to " + quote(to), e);
    }
}
 
Example 6
Source File: ThemeBuilderUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Copies all the contents from the directory given by the source path to the directory given by the
 * target path
 *
 * <p>
 * If source directory does not exist nothing is performed. The target directory will be created if it
 * does not exist. Any hidden directories (directory names that start with ".") will be deleted from the
 * target directory
 * </p>
 *
 * @param sourceDirectoryPath absolute path to the source directory
 * @param targetDirectoryPath absolute path to the target directory
 * @throws IOException
 */
public static void copyDirectory(String sourceDirectoryPath, String targetDirectoryPath)
        throws IOException {
    File sourceDir = new File(sourceDirectoryPath);

    if (!sourceDir.exists()) {
        return;
    }

    File targetDir = new File(targetDirectoryPath);
    if (targetDir.exists()) {
        // force removal so the copy starts clean
        FileUtils.forceDelete(targetDir);
    }

    targetDir.mkdir();

    FileUtils.copyDirectoryStructure(sourceDir, targetDir);

    // remove hidden directories from the target
    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setBasedir(targetDir);

    scanner.scan();

    for (String includedDirectory : scanner.getIncludedDirectories()) {
        File subdirectory = new File(targetDir, includedDirectory);

        if (subdirectory.exists() && subdirectory.isDirectory()) {
            if (subdirectory.getName().startsWith(".")) {
                FileUtils.forceDelete(subdirectory);
            }
        }
    }
}
 
Example 7
Source File: CreateServerMojo.java    From ci.maven with Apache License 2.0 5 votes vote down vote up
@Override
protected void doExecute() throws Exception {
    if (skip) {
        return;
    }
    if (isInstall) {
        installServerAssembly();
    } else {
        log.info(MessageFormat.format(messages.getString("info.install.type.preexisting"), ""));
        checkServerHomeExists();
    }

    boolean createServer = false;

    if (!serverDirectory.exists()) {
        createServer = true;
    } else if (refresh) {
        FileUtils.forceDelete(serverDirectory);
        createServer = true;
    }

    if (createServer) {
        // server does not exist or we are refreshing it - create it
        log.info(MessageFormat.format(messages.getString("info.server.start.create"), serverName));
        ServerTask serverTask = initializeJava();
        serverTask.setOperation("create");
        serverTask.setTemplate(template);
        serverTask.setNoPassword(noPassword);
        serverTask.execute();
        log.info(MessageFormat.format(messages.getString("info.server.create.created"), serverName, serverDirectory.getCanonicalPath()));
    }
    
    // copy files _after_ we create the server
    copyConfigFiles();

    copyLibertySettings();
}
 
Example 8
Source File: MojoExtension.java    From helm-maven-plugin with MIT License 4 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) throws Exception {
    FileUtils.forceDelete(getProjectBuildDirectory(context).toFile());
}
 
Example 9
Source File: Utils.java    From usergrid with Apache License 2.0 4 votes vote down vote up
/**
 * Gets all dependency jars of the project specified by 'project' parameter from the local mirror and copies them
 * under targetFolder
 *
 * @param targetFolder The folder which the dependency jars will be copied to
 */
public static void copyArtifactsTo( MavenProject project, String targetFolder )
        throws MojoExecutionException {
    File targetFolderFile = new File( targetFolder );
    for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); ) {
        Artifact artifact = ( Artifact ) it.next();

        File f = artifact.getFile();

        LOG.info( "Artifact {} found.", f.getAbsolutePath() );

        if ( f == null ) {
            throw new MojoExecutionException( "Cannot locate artifact file of " + artifact.getArtifactId() );
        }

        // Check already existing artifacts and replace them if they are of a lower version
        try {

            List<String> existing =
                    FileUtils.getFileNames( targetFolderFile, artifact.getArtifactId() + "-*.jar", null, false );

            if ( existing.size() != 0 ) {
                String version =
                        existing.get( 0 ).split( "(" + artifact.getArtifactId() + "-)" )[1].split( "(.jar)" )[0];
                DefaultArtifactVersion existingVersion = new DefaultArtifactVersion( version );
                DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion( artifact.getVersion() );

                if ( existingVersion.compareTo( artifactVersion ) < 0 ) { // Remove existing version
                    FileUtils.forceDelete( targetFolder + existing.get( 0 ) );
                }
                else {
                    LOG.info( "Artifact " + artifact.getArtifactId() + " with the same or higher " +
                            "version already exists in lib folder, skipping copy" );
                    continue;
                }
            }

            LOG.info( "Copying {} to {}", f.getName(), targetFolder );
            FileUtils.copyFileToDirectory( f.getAbsolutePath(), targetFolder );
        }
        catch ( IOException e ) {
            throw new MojoExecutionException( "Error while copying artifact file of " + artifact.getArtifactId(),
                    e );
        }
    }
}
 
Example 10
Source File: BasicSupport.java    From ci.maven with Apache License 2.0 4 votes vote down vote up
protected void installFromFile() throws Exception {
    // Check if there is a different/newer archive or missing marker to trigger assembly install
    File installMarker = new File(installDirectory, ".installed");

    if (!refresh) {
        if (!installMarker.exists()) {
            refresh = true;
        } else if (assemblyArchive.lastModified() > installMarker.lastModified()) {
            log.debug(MessageFormat.format(messages.getString("debug.detect.assembly.archive"), ""));
            refresh = true;
        } else if(!assemblyArchive.getCanonicalPath().equals(FileUtils.fileRead(installMarker))) {
            refresh = true;
        }
    } else {
        log.debug(MessageFormat.format(messages.getString("debug.request.refresh"), ""));
    }

    String userDirectoryPath = userDirectory.getCanonicalPath();
    if (refresh && installDirectory.exists() && installDirectory.isDirectory()) {
        log.info(MessageFormat.format(messages.getString("info.uninstalling.server.home"), installDirectory));
        // Delete everything in the install directory except usr directory
        for(File f : installDirectory.listFiles()) {
            if(!(f.isDirectory() && f.getCanonicalPath().equals(userDirectoryPath))) {
                FileUtils.forceDelete(f);
            }
        }
    }

    // Install the assembly
    if (!installMarker.exists()) {
        log.info("Installing assembly...");

        FileUtils.forceMkdir(installDirectory);

        Expand unzip = (Expand) ant.createTask("unzip");

        unzip.setSrc(assemblyArchive);
        unzip.setDest(assemblyInstallDirectory.getCanonicalFile());
        unzip.execute();

        // Make scripts executable, since Java unzip ignores perms
        Chmod chmod = (Chmod) ant.createTask("chmod");
        chmod.setPerm("ugo+rx");
        chmod.setDir(installDirectory);
        chmod.setIncludes("bin/*");
        chmod.setExcludes("bin/*.bat");
        chmod.execute();

        // delete installMarker first in case it was packaged with the assembly
        installMarker.delete();
        installMarker.createNewFile();
        
        // Write the assembly archive path so we can determine whether to install a different assembly in future invocations
        FileUtils.fileWrite(installMarker, assemblyArchive.getCanonicalPath());
    } else {
        log.info(MessageFormat.format(messages.getString("info.reuse.installed.assembly"), ""));
    }
}