十二猴子 3.1.1 || 3.1.2 不使用 maven-dependency-plugin
TwelveMonkeys 3.1.1 || 3.1.2 not working with maven-dependency-plugin
我正在尝试在我的应用程序中使用 TwelveMonkeys 来处理缩略图生成。此外,我正在使用 maven-dependency-plugin
解压所有依赖项并创建一个 FAT jar。虽然,插件在正常情况下工作得很好,但在与 maven-dependency-plugin 一起使用时根本不起作用。这是我的 POM XML:
<?xml version="1.0" encoding="UTF-8"?>
<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.prakhar</groupId>
<artifactId>demo</artifactId>
<name>demo</name>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>
<properties>
<slf4j.version>1.6.1</slf4j.version>
</properties>
<build>
<plugins>
<plugin>
<!-- Get the dependencies jar files to a common place, for jar signing -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<!-- <includeArtifactIds>commons-net,imageio-bmp,imageio-jpeg,imageio-tiff,imageio-pnm,imageio-psd,imageio-iff,imageio-pcx,imageio-pict,imageio-sgi,imageio-tga,imageio-icns,imageio-thumbsdb,imageio-core,imageio-metadata,common-lang,common-io,common-image</includeArtifactIds> -->
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- MigLayout -->
<!-- Apache Commons -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Twelve Monkeys dependencies -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pnm</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-psd</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-iff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pcx</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pict</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-sgi</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tga</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-icns</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-thumbsdb</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
</project>
而且,这是我的功能:
private static void convertToJpg(File file) {
try {
BufferedImage image = ImageIO.read(file);
int width = 300, height = 200; // new width/height
BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS); // A good default filter, see class documentation for more info
BufferedImage thumbnailImg = resampler.filter(image, null);
FileImageOutputStream fios = new FileImageOutputStream(new File(file.getAbsolutePath() + ".png"));
if (!ImageIO.write(thumbnailImg, "PNG", fios)) {
System.out.println("ERROR: Unable to convert image " + file.getAbsolutePath());
} else {
System.out.println("SUCCESS: Image " + file.getAbsolutePath() + ".png converted successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
}
这是我得到的异常:
java.lang.NullPointerException: Input == null
at com.twelvemonkeys.image.ResampleOp.filter(Unknown Source)
如果有人知道如何解决,请说明。
综上所述,对于以后的读者来说,问题是每个ImageIO插件JAR都包含META-INF/services
下的多个资源。自动发现 ImageIO 插件需要这些资源。他们是:
META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi
META-INF/services/javax.imageio.spi.ImageInpuStreamSpi
META-INF/services/javax.imageio.spi.ImageOutputStreamSpi
可能还有更多,但 TwelveMonkeys 没有使用这些。
如有兴趣,您可以阅读更多关于 Service Provider Interface mechanism and the ServiceRegistry
class 的内容。
当您将所有 JAR 文件解压缩到一个文件夹中时,每个 JAR 都会覆盖这些文件。相反,需要将具有相同名称的资源合并为一个新的单一资源。
Maven Shade plugin provides a solution for this, see Resource transformers, especially the ServicesResourceTransformer。
我正在尝试在我的应用程序中使用 TwelveMonkeys 来处理缩略图生成。此外,我正在使用 maven-dependency-plugin
解压所有依赖项并创建一个 FAT jar。虽然,插件在正常情况下工作得很好,但在与 maven-dependency-plugin 一起使用时根本不起作用。这是我的 POM XML:
<?xml version="1.0" encoding="UTF-8"?>
<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.prakhar</groupId>
<artifactId>demo</artifactId>
<name>demo</name>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>
<properties>
<slf4j.version>1.6.1</slf4j.version>
</properties>
<build>
<plugins>
<plugin>
<!-- Get the dependencies jar files to a common place, for jar signing -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<!-- <includeArtifactIds>commons-net,imageio-bmp,imageio-jpeg,imageio-tiff,imageio-pnm,imageio-psd,imageio-iff,imageio-pcx,imageio-pict,imageio-sgi,imageio-tga,imageio-icns,imageio-thumbsdb,imageio-core,imageio-metadata,common-lang,common-io,common-image</includeArtifactIds> -->
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- MigLayout -->
<!-- Apache Commons -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Twelve Monkeys dependencies -->
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tiff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pnm</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-psd</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-iff</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pcx</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-pict</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-sgi</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-tga</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-icns</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-thumbsdb</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
</project>
而且,这是我的功能:
private static void convertToJpg(File file) {
try {
BufferedImage image = ImageIO.read(file);
int width = 300, height = 200; // new width/height
BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS); // A good default filter, see class documentation for more info
BufferedImage thumbnailImg = resampler.filter(image, null);
FileImageOutputStream fios = new FileImageOutputStream(new File(file.getAbsolutePath() + ".png"));
if (!ImageIO.write(thumbnailImg, "PNG", fios)) {
System.out.println("ERROR: Unable to convert image " + file.getAbsolutePath());
} else {
System.out.println("SUCCESS: Image " + file.getAbsolutePath() + ".png converted successfully");
}
} catch (Exception e) {
e.printStackTrace();
}
}
这是我得到的异常:
java.lang.NullPointerException: Input == null
at com.twelvemonkeys.image.ResampleOp.filter(Unknown Source)
如果有人知道如何解决,请说明。
综上所述,对于以后的读者来说,问题是每个ImageIO插件JAR都包含META-INF/services
下的多个资源。自动发现 ImageIO 插件需要这些资源。他们是:
META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi
META-INF/services/javax.imageio.spi.ImageInpuStreamSpi
META-INF/services/javax.imageio.spi.ImageOutputStreamSpi
可能还有更多,但 TwelveMonkeys 没有使用这些。
如有兴趣,您可以阅读更多关于 Service Provider Interface mechanism and the ServiceRegistry
class 的内容。
当您将所有 JAR 文件解压缩到一个文件夹中时,每个 JAR 都会覆盖这些文件。相反,需要将具有相同名称的资源合并为一个新的单一资源。
Maven Shade plugin provides a solution for this, see Resource transformers, especially the ServicesResourceTransformer。