2

我正在尝试使用 springboot java 框架编译集成服务器。我正在使用 Maven 编译一个 jar 文件,但我一直遇到 Maven 在存储库中存储 jar 的方式的问题。

我正在使用的 Jar 是 sapjco3.jar,它会在运行(而不是包)时崩溃,因为它需要它的名称是 sapjco3.jar。因为 Maven 存储库命名约定是:

/%groupId%/%artifactId%/%version%/%artifactId%-%version%.jar

该库最终被称为 sapjco3-3.0.jar。

有没有办法在不使用存储库的情况下将我的 sapjco3.jar 弹出到我的应用程序中(我无法将它添加到 java.library.path 或通过编辑 ~/.m2 目录中的条目和 xml)。否则,有没有办法在插入后对其进行脚本重命名?

顺便说一下,我的平台是 ubuntu 14.04 LTS。

我的 POM 文件(缺少 xml 标头,因为 StackOverflow 无法正确解析它):

<modelVersion>4.0.0</modelVersion>

<name>SampleIntegrationServer</name>
<description>Sample Integration Server</description>
<groupId>com.Sample</groupId>
<artifactId>SampleIntegrationServer</artifactId>
<packaging>jar</packaging>
<version>0.0.2</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <spring-cloud-aws-version>1.0.4.RELEASE</spring-cloud-aws-version>
</properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-ws</artifactId>
        </dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-velocity</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
            <version>${spring-cloud-aws-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-aws-messaging</artifactId>
            <version>${spring-cloud-aws-version}</version>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.jt400</groupId>
            <artifactId>jt400</artifactId>
            <version>6.7</version>
        </dependency>
        <dependency>
            <groupId>com.sap</groupId>
            <artifactId>sapjco3</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.2</version>
        </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.3.RELEASE</version>
        </plugin>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classes</outputDirectory>
                        <includeEmptyDirs>true</includeEmptyDirs>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
        </plugin>
    </plugins>
</build>

迷恋;撞车;崩溃:

Factory method 'createSapConnection' threw exception; nested exception is java.lang.ExceptionInInitializerError: JCo initialization failed with java.lang.ExceptionInInitializerError: Illegal JCo archive "sapjco3-3.0.jar". It is not allowed to rename or repackage the original archive "sapjco3.jar".

我只是想建立这个!

谢谢

4

2 回答 2

2

我用自定义布局解决了这个问题

import org.springframework.boot.loader.tools.Layouts.Jar;

public class ResistantToSapjco3JarLayout extends Jar implements CustomLoaderLayout {

  @Override
  public void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {

    // do Spring Boot defaults
    writer.writeLoaderClasses();

    URL jcoClassUrl = getClass().getClassLoader().getResource("com/sap/conn/jco/JCo.class");
    if (jcoClassUrl == null) {
        throw new IllegalStateException("No sapjco3 JAR on plugin classpath. "
                + "Make sure sapjco3 is added to spring-boot-maven-plugin dependency section");
    }
    if (!jcoClassUrl.toString().startsWith("jar:file")) {
        throw new IllegalStateException("sapjco3 is not JAR");
    }
    JarURLConnection jarUrlConnection = (JarURLConnection) jcoClassUrl.openConnection();
    JarFile sapjco3Jar = jarUrlConnection.getJarFile();

    JarWriter jarWriter = (JarWriter) writer;
    jarWriter.writeNestedLibrary("BOOT-INF/lib/",
      new Library("sapjco3.jar", new File(sapjco3Jar.getName()), LibraryScope.COMPILE, false));

  }

}

pom.xml

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <layoutFactory implementation="com.company.ResistantToSapjco3JarLayoutFactory" />
      <excludeArtifactIds>sapjco3</excludeArtifactIds>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>resistant-to-sapjco3</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>sapjco3</groupId>
            <artifactId>sapjco3</artifactId>
            <version>3.0.13</version>
        </dependency>
    </dependencies>
</plugin>

ResistantToSapjco3JarLayoutFactory

public class ResistantToSapjco3JarLayoutFactory implements LayoutFactory {

  @Override
  public Layout getLayout(File source) {
        return new ResistantToSapjco3JarLayout();
  }
}
于 2017-06-13T13:30:10.737 回答
0

我使用 maven 的 shade 插件解决了这个问题。对 sapjco3 具有 maven 依赖项并提供范围:

<dependency>
  <groupId>com.sap.conn.jco</groupId>
  <artifactId>sapjco3</artifactId>
  <version>${sapjco3.version}</version>
  <scope>provided</scope>
</dependency>

然后通过在shading时复制库,在spring maven插件添加它的版本时排除它来完成工作,这样我们就有了原来的:

<build>
<finalName>price</finalName>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy-dependencies</id>
        <phase>process-resources</phase>
        <goals>
          <goal>copy-dependencies</goal>
        </goals>
        <configuration>
          <stripVersion>true</stripVersion>
          <includeArtifactIds>sapjco3</includeArtifactIds>
          <includeTypes>jar</includeTypes>
          <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
      <execution>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
          <createDependencyReducedPom>true</createDependencyReducedPom>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
              <resource>BOOT-INF/lib/sapjco3.jar</resource>
              <file>sapjco3.jar</file>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.7.RELEASE</version>
    <configuration>
      <excludeGroupIds>com.sap.conn.jco</excludeGroupIds>
    </configuration>
  </plugin>
 ...
于 2019-09-10T09:17:45.903 回答