2014-05-19 39 views
1

我有一套JAR。它可以是任意数量的。有没有可以将文件复制到每个JAR中的插件(如truezip-maven-plugin或maven-resources-plugin)?从我看到的truezip变得最接近这个,但我必须明确指定JAR。Maven插件可以将文件复制到多个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来说,推荐的方法是什么?

更新:我做了我自己的插件,这是否

感谢, 张志贤

+0

你有一个项目与多个罐子或项目与子项目和每个子项目有一个罐子? –

+0

我有多个项目,每个项目都有一个罐子 – Sandman

+0

然后,你应该只配置任何可以在父pom上使用资源(不是jar)的插件,例如标准资源插件可以使用额外的文件夹作为源代码,或者更容易使用build-帮手插件 –

回答

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文件的路径。

相关问题