这将是 Maven 重新打包方法:
- 根据下面的 pom.xml 创建一个 maven 项目
- 添加 Java 类 Rasterizer
- 添加 JUnit 测试 TestRasterizer
- 添加示例文件 readandblue.svg
- 运行 mvn 安装
你应该得到一个“阴影”jar 文件 target/com.bitplan.rasterizer-0.0.1.jar 已经包含了 java.awt.headless 调用:
您也可以使用 pom.xml “独立”并简单地更改其中的主类。
Rasterize.java 包装类
package com.bitplan.rasterizer;
import org.apache.batik.apps.rasterizer.Main;
/**
* Rasterizer main class
* https://stackoverflow.com/questions/13112967/how-to-call-batik-rasterizer-jar
* @author wf
*
*/
public class Rasterizer {
/**
* wrapper entry point for batik rasterizer
* @param args
*/
public static void main(String[] args) {
System.setProperty("java.awt.headless", "true");
// see
// http://www.docjar.org/html/api/org/apache/batik/apps/rasterizer/Main.java.html
Main main=new Main(args);
main.execute();
}
}
TestRasterizer.java JUnit 测试用例
package com.bitplan.rasterizer;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
/**
* test the rasterizer
* @author wf
*
*/
public class TestRasterizer {
@Test
public void testRasterizer() {
File testFile=new File("src/test/data/redandblue.svg");
File pngFile=new File("src/test/data/redandblue.png");
if (pngFile.exists())
pngFile.delete();
assertTrue(testFile.exists());
String [] args={"-scriptSecurityOff","-m","image/png","-q","0.8",testFile.getPath()};
com.bitplan.rasterizer.Rasterizer.main(args);
assertTrue(pngFile.exists());
}
}
示例 svg 文件:readandblue.svg
<svg xmlns="http://www.w3.org/2000/svg"
width="467" height="462">
<rect x="80" y="60" width="250" height="250" rx="20"
style="fill:#ff0000; stroke:#000000;stroke-width:2px;" />
<rect x="140" y="120" width="250" height="250" rx="40"
style="fill:#0000ff; stroke:#000000; stroke-width:2px;
fill-opacity:0.7;" />
</svg>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bitplan.java</groupId>
<version>0.0.1</version>
<artifactId>com.bitplan.rasterizer</artifactId>
<name>com.bitplan.rasterizer</name>
<description>Provide rasterizer access</description>
<properties>
<junit3.version>3.8.1</junit3.version>
<junit4.version>4.11</junit4.version>
</properties>
<dependencies>
<!-- batik rasterizer -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-rasterizer</artifactId>
<version>1.7</version>
</dependency>
<!-- https://issues.apache.org/bugzilla/show_bug.cgi?id=44682 -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.7</version>
</dependency>
<!-- Runtime Junit access -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit4.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- <mainClass>org.apache.batik.apps.rasterizer.Main</mainClass> -->
<mainClass>com.bitplan.rasterizer.Rasterizer</mainClass> -->
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>