<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做推荐的方法是什么?
更新:我制作了自己的插件,这样做
谢谢, TEO
答案 0 :(得分: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的文件的路径。