protected File changeVersion()

in src/main/java/org/apache/sling/maven/bundlesupport/AbstractBundleDeployMojo.java [123:153]


    protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException {
        String fileName = file.getName();
        int pos = fileName.indexOf(oldVersion);
        fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length());
        final File destJar = new File(file.getParentFile(), fileName);

        // now create a temporary file and update the version
        try (JarFile sourceJar = new JarFile(file)) {
            final Manifest manifest = sourceJar.getManifest();
            manifest.getMainAttributes().putValue(Constants.BUNDLE_VERSION, newVersion);
            try (OutputStream out = new FileOutputStream(destJar);
                    JarOutputStream jos = new JarOutputStream(out, manifest)) {
                jos.setMethod(ZipOutputStream.DEFLATED);
                jos.setLevel(Deflater.BEST_COMPRESSION);
                Enumeration<JarEntry> entries = sourceJar.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entryIn = entries.nextElement();
                    JarEntry entryOut = new JarEntry(entryIn);
                    jos.putNextEntry(entryOut);
                    if (!entryIn.isDirectory()) {
                        try (InputStream jis = sourceJar.getInputStream(entryOut)) {
                            IOUtils.copy(jis, jos);
                        }
                    }
                }
                return destJar;
            }
        } catch (IOException ioe) {
            throw new MojoExecutionException("Unable to update version in jar file.", ioe);
        }
    }