有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Bouncy Castle未签名项使用Maven运行Jarwithdependencies时出错

我目前在尝试运行使用bouncy castle进行解密的可执行jar时遇到问题。使用maven,我创建了一个jar,其中包含运行时抛出的依赖项

java.util.jar.JarException: file: FileDecrypter-1.0-jar-with-dependencies.jar 
has unsigned entries - org/apache/commons/io/ByteOrderMark.class

当然,我研究了这个问题,发现了"Invalid signature file" when attempting to run a .jar,但我没有创建uber jar,也没有使用清单文件来创建我的jar

我的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>FileDecrypter</groupId>
<artifactId>FileDecrypter</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.3</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.48</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpg-jdk15on</artifactId>
    <version>1.48</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
 <resources>
  <resource>
    <directory>src</directory>
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
  </resource>
 </resources>

 <plugins>
  <!-- download source code in Eclipse, best practice -->
 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <downloadSources>true</downloadSources>
        <downloadJavadocs>false</downloadJavadocs>
    </configuration>
</plugin>

 <!-- Make this jar executable -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <excludes>
            <exclude>**/log4j.properties</exclude>
        </excludes>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>com.mps.main.DecryptInput</mainClass>
                <classpathPrefix>dependency-jars/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin>

<!-- Copy project dependency -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!-- exclude junit, we need runtime dependency only -->
                <includeScope>runtime</includeScope>
                <outputDirectory>${project.build.directory}/dependency-  
jars/</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<!-- set jdk compiler -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <verbose>true</verbose>
      <fork>true</fork>
      <executable>${JAVA_1_7_HOME}</executable>
      <compilerVersion>1.7</compilerVersion>
    </configuration>
</plugin>

 <!-- Maven Assembly Plugin -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <!-- get all project dependencies -->
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <!-- MainClass in mainfest make a executable jar -->
        <archive>
          <manifest>
            <mainClass>com.mps.main.DecryptInput</mainClass>
          </manifest>
        </archive>

    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
                                  <!-- bind to the packaging phase -->
        <phase>package</phase> 
        <goals>
            <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>
</plugins>
</build>
</project>

共 (1) 个答案

  1. # 1 楼答案

    虽然我用错误的方法处理这个问题,但我最终还是让它工作了。在我的pom中使用以下代码。xml,我能够创建一个jar,其中包含指向我的依赖项的清单文件:

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.5.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!  exclude junit, we need runtime dependency only  >
                <includeScope>runtime</includeScope>
                <outputDirectory>${project.build.directory}/dependency-  
    jars/</outputDirectory>
            </configuration>
        </execution>
    </executions>
    </plugin>
    

    使用我的pom。xml并在命令行中运行“mvn package”,而我当前的工作目录是我当前的项目工作区,创建了一个名为“target”的文件夹,其中包含我的jar文件“FileDecrypter.jar”和一个包含我所有jar的文件夹“dependency jars”。executejar中的清单文件指向dependency jar文件夹,它允许bouncy castle jar保持签名状态