【问题标题】:What Maven plugin can copy a file into multiple JARs?什么 Maven 插件可以将一个文件复制到多个 JAR 中?
【发布时间】:2015-07-11 23:31:15
【问题描述】:

我有一组 JAR。可以是任意数量。有没有我可以使用的插件(如 truezip-maven-plugin 或 maven-resources-plugin)可以将文件复制到每个 JAR 中?从我所见,truezip 最接近于此,但我必须明确指定 JAR。

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>truezip-maven-plugin</artifactId>
  <version>1.1</version>
  <executions>
    <execution>
      <id>copy-my-file</id>
      <goals>
        <goal>copy</goal>
      </goals>
      <phase>process-sources</phase>
      <configuration>
        <from>${somefolder}/myfile.txt</from>
        <to>${project.build.directory}/*.jar/folderInsideJar/myfile.txt</to>
      </configuration>
    </execution>
  </executions>
</plugin>

通配符 *.jar 是否正确?它会这样工作吗?如果不是,对于多个 JAR 的推荐方法是什么?

更新:我做了我自己的插件来做这个

谢谢, 特奥

【问题讨论】:

  • 你有一个项目有多个 jars 或项目有子项目,每个子项目都有一个 jar?
  • 我有多个项目,每个项目都有一个 jar
  • 那么你应该只配置任何在父 pom 上使用资源(不是 jars)的插件,例如标准资源插件可以使用额外的文件夹作为源,或者更容易使用 build-helper-plugin
  • 是的,这将是更简单的出路,但不幸的是,我不希望在第一次构建 jar 时发生这种情况,我希望在稍后阶段进行。无论如何,我发现制作一个 Maven 插件一点也不难,我就是这样做的:)
  • @Teo Care 要分享你的答案吗?

标签: maven maven-plugin


【解决方案1】:

为了发布我自己的解决方案:我创建了一个简单的 Maven 插件:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

public class MyMojo extends AbstractMojo {

    /**
     * @parameter The directory which contains JARs to be signed
     */
    private String directory;

    /**
     * @parameter The name of the template to sign into the JARs
     */
    private String template;

    public void execute() throws MojoExecutionException {
        final File dir = new File(directory);
        final String[] jars = dir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        });
        final File jnlpTemplate = new File(template);
        byte[] templateBytes = null;
        try {
            templateBytes = toByteArray(jnlpTemplate);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        for (String jar : jars) {
            try {
                File currentJar = new File(dir, jar);
                File newJar = new File(dir, "temp.jar");
                ZipFile old = new ZipFile(currentJar);
                ZipOutputStream append = new ZipOutputStream(
                        new FileOutputStream(newJar));

                // first, copy contents from existing jar to new one
                Enumeration<? extends ZipEntry> entries = old.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry e = entries.nextElement();
                    append.putNextEntry(e);
                    if (!e.isDirectory()) {
                        copy(old.getInputStream(e), append);
                    }
                    append.closeEntry();
                }

                // now append some extra content
                ZipEntry e = new ZipEntry("JNLP-INF/" + jnlpTemplate.getName());
                append.putNextEntry(e);
                append.write(templateBytes);
                append.closeEntry();

                // close
                old.close();
                append.close();
                currentJar.delete();
                newJar.renameTo(new File(dir,jar));
            } catch (Throwable t) {
                System.out.println("Could not sign " + jar + ": "
                        + t.getMessage());
            }
        }

    }

    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    /**
     * copy input to output stream - available in several StreamUtils or Streams
     * classes
     */
    public static void copy(InputStream input, OutputStream output)
            throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER)) != -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }

    public static byte[] toByteArray(File f) throws IOException {
        if (f.length() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(f + " is too large!");
        }
        int length = (int) f.length();
        byte[] content = new byte[length];
        int off = 0;
        int read = 0;
        InputStream in = new FileInputStream(f);
        try {
            while (read != -1 && off < length) {
                read = in.read(content, off, (length - off));
                off += read;
            }
            if (off != length) {
                // file size has shrunken since check, handle appropriately
            } else if (in.read() != -1) {
                // file size has grown since check, handle appropriately
            }
            return content;
        } finally {
            in.close();
        }
    }
}

那是很久以前的事了 :) 但基本思想是:您可以将 2 个参数传递给 Mojo,一个充满 JAR 的目录以及应该放入每个 JAR 中的文件的路径。

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 2014-06-21
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多