Java Code Examples for org.codehaus.plexus.util.IOUtil#copy()

The following examples show how to use org.codehaus.plexus.util.IOUtil#copy() . 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: AgentJar.java    From promagent with Apache License 2.0 6 votes vote down vote up
void extractJar(File jar, ManifestTransformer manifestTransformer) throws MojoExecutionException {
    try (JarFile jarFile = new JarFile(jar)) {
        for (Enumeration<JarEntry> jarEntries = jarFile.entries(); jarEntries.hasMoreElements(); ) {
            JarEntry jarEntry = jarEntries.nextElement();
            if (manifestTransformer.canTransform(jarEntry)) {
                jarEntry = manifestTransformer.transform(jarEntry);
            }
            if (!jarEntry.isDirectory() && !content.contains(jarEntry.getName())) {
                content.add(jarEntry.getName());
                makeDirsRecursively(jarEntry.getName());
                try (InputStream in = getInputStream(jarEntry, jarFile, manifestTransformer)) {
                    jarOutputStream.putNextEntry(jarEntry);
                    IOUtil.copy(in, jarOutputStream);
                }
            }
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error adding " + jar.getName() + " to target JAR: " + e.getMessage(), e);
    }
}
 
Example 2
Source File: AbstractDependencyTest.java    From maven-native with MIT License 6 votes vote down vote up
protected void writeFile( String filePath, String content )
    throws IOException
{
    FileOutputStream fs = new FileOutputStream( filePath );
    try
    {
        IOUtil.copy( content, fs );
    }
    finally
    {
        if ( fs != null )
        {
            IOUtil.close( fs );
        }
    }
}
 
Example 3
Source File: Platform.java    From appassembler with MIT License 6 votes vote down vote up
private String interpolateBaseDirAndRepo( String content )
{
    StringReader sr = new StringReader( content );
    StringWriter result = new StringWriter();

    Map<Object, Object> context = new HashMap<Object, Object>();

    context.put( "BASEDIR", StringUtils.quoteAndEscape( getBasedir(), '"' ) );
    context.put( "REPO", StringUtils.quoteAndEscape( getRepo(), '"' ) );
    InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( sr, context, "@", "@" );
    try
    {
        IOUtil.copy( interpolationFilterReader, result );
    }
    catch ( IOException e )
    {
        // shouldn't happen...
    }
    return result.toString();
}
 
Example 4
Source File: JavaServiceWrapperDaemonGenerator.java    From appassembler with MIT License 6 votes vote down vote up
private static void writeFile( File outputFile, Reader reader )
    throws DaemonGeneratorException
{
    FileWriter out = null;

    try
    {
        outputFile.getParentFile().mkdirs();
        out = new FileWriter( outputFile );

        IOUtil.copy( reader, out );
    }
    catch ( IOException e )
    {
        throw new DaemonGeneratorException( "Error writing output file: " + outputFile.getAbsolutePath(), e );
    }
    finally
    {
        IOUtil.close( reader );
        IOUtil.close( out );
    }
}
 
Example 5
Source File: FormattedPropertiesTest.java    From appassembler with MIT License 6 votes vote down vote up
private void saveAndCompare( String expectedResource )
    throws IOException
{
    StringOutputStream string = new StringOutputStream();
    formattedProperties.save( string );

    StringOutputStream expected = new StringOutputStream();
    InputStream asStream = getClass().getResourceAsStream( expectedResource );
    try
    {
        IOUtil.copy( asStream, expected );
    }
    finally
    {
        IOUtil.close( asStream );
    }

    String unified = StringUtils.unifyLineSeparators( expected.toString() );
    assertEquals( unified, string.toString() );
}
 
Example 6
Source File: AgentJar.java    From promagent with Apache License 2.0 5 votes vote down vote up
void addFile(File srcFile, String targetFileName, Directory targetDir) throws MojoExecutionException {
    String destPath = targetDir.getName() + targetFileName;
    if (content.contains(destPath)) {
        return;
    }
    makeDirsRecursively(destPath);
    content.add(destPath);
    try (InputStream in = new FileInputStream(srcFile)) {
        jarOutputStream.putNextEntry(new JarEntry(destPath));
        IOUtil.copy(in, jarOutputStream);
    } catch (IOException e) {
        throw new MojoExecutionException("Error adding " + srcFile.getName() + " to target JAR: " + e.getMessage(), e);
    }
}
 
Example 7
Source File: AbstractVersionsUpdaterMojo.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Writes a StringBuilder into a file.
 *
 * @param outFile The file to read.
 * @param input The contents of the file.
 * @throws IOException when things go wrong.
 */
protected final void writeFile( File outFile, StringBuilder input )
    throws IOException
{
    Writer writer = WriterFactory.newXmlWriter( outFile );
    try
    {
        IOUtil.copy( input.toString(), writer );
    }
    finally
    {
        IOUtil.close( writer );
    }
}
 
Example 8
Source File: AppassemblerBooter.java    From appassembler with MIT License 5 votes vote down vote up
private static String interpolateBaseDirAndRepo( String content )
{
    StringReader sr = new StringReader( content );
    StringWriter result = new StringWriter();

    Map<Object, Object> context = new HashMap<Object, Object>();

    final String baseDir = System.getProperty( "basedir", System.getProperty( "app.home" ) );
    if ( baseDir != null && baseDir.length() > 0) {
        context.put( "BASEDIR", StringUtils.quoteAndEscape(baseDir, '"') );
    }

    final String repo = System.getProperty( "app.repo" );
    if ( repo != null && repo.length() > 0 ) {
        context.put("REPO", StringUtils.quoteAndEscape(repo, '"'));
    }

    InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( sr, context, "@", "@" );
    try
    {
        IOUtil.copy( interpolationFilterReader, result );
    }
    catch ( IOException e )
    {
        // shouldn't happen...
    }
    return result.toString();
}
 
Example 9
Source File: AbstractDaemonGeneratorTest.java    From appassembler with MIT License 5 votes vote down vote up
protected File createFilteredFile( String file )
    throws IOException, FileNotFoundException, DaemonGeneratorException, XmlPullParserException
{
    String version = getAppAssemblerBooterVersion();
    Properties context = new Properties();
    context.setProperty( "appassembler.version", version );

    File tempPom = File.createTempFile( "appassembler", "tmp" );
    tempPom.deleteOnExit();

    InterpolationFilterReader reader =
        new InterpolationFilterReader( new FileReader( getTestFile( file ) ), context, "@", "@" );
    FileWriter out = null;

    try
    {
        out = new FileWriter( tempPom );

        IOUtil.copy( reader, out );
    }
    catch ( IOException e )
    {
        throw new DaemonGeneratorException( "Error writing output file: " + tempPom.getAbsolutePath(), e );
    }
    finally
    {
        IOUtil.close( reader );
        IOUtil.close( out );
    }
    return tempPom;
}
 
Example 10
Source File: DockerFileUtilTest.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
private File copyToTempDir(String resource) throws IOException {
    File dir = Files.createTempDirectory("d-m-p").toFile();
    File ret = new File(dir, "Dockerfile");
    try (FileOutputStream os = new FileOutputStream(ret)) {
        IOUtil.copy(getClass().getResourceAsStream(resource), os);
    }
    return ret;
}
 
Example 11
Source File: Mojo.java    From capsule-maven-plugin with MIT License 5 votes vote down vote up
JarOutputStream addToJar(final String name, final InputStream input, final JarOutputStream jar) throws IOException {
	try {
		debug("\t[Added to Jar]: " + name);
		jar.putNextEntry(new ZipEntry(name));
		IOUtil.copy(input, jar);
		jar.closeEntry();
	} catch (final ZipException ignore) {} // ignore duplicate entries and other errors
	IOUtil.close(input);
	return jar;
}
 
Example 12
Source File: Analyzer.java    From Moss with Apache License 2.0 4 votes vote down vote up
private static void readPomInfo(String location, List<PomInfo> pomInfos) {
    Properties properties = new Properties();
    String metaPath = "META-INF";
    try {
        InputStream is;
        if (location.contains("!")) {
            // read inside jar's
            is = new ClassPathResource(location.substring(location.indexOf("!") + 1)).getInputStream();
        } else {
            URL realUrl = new URL(location);
            is = realUrl.openStream();
        }

        PomInfo pomInfo = null;
        ZipInputStream zip = new ZipInputStream(is);
        ZipEntry zipEntry;
        while ((zipEntry = zip.getNextEntry()) != null) {
            String zipEntryPath = zipEntry.getName();
            if (zipEntryPath.startsWith(metaPath + "/maven") && zipEntryPath.endsWith("pom.xml")) {
                logger.debug("zipEntryPath: " + zipEntryPath);
                pomInfo = readPom(zip);
                break;
            }

            if (zipEntryPath.equals(metaPath + "/MANIFEST.MF")) {
                properties.load(zip);
            }
        }

        // calculate jar size
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtil.copy(is, output);
        long jarSize = output.toByteArray().length;

        if (null != pomInfo) {
            pomInfo.setLocation(location);
            pomInfo.setSize(jarSize);
            pomInfos.add(pomInfo);
        }
    } catch (Exception e) {
        logger.error("get jar maven pom failed! location:" + location, e);
    }
}
 
Example 13
Source File: AuthConfigFactoryTest.java    From docker-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void createOpenShiftConfig(File homeDir,String testConfig) throws IOException {
    File kubeDir = new File(homeDir,".kube");
    kubeDir.mkdirs();
    File config = new File(kubeDir,"config");
    IOUtil.copy(getClass().getResourceAsStream(testConfig),new FileWriter(config));
}