Библиотеки не загружены в исполняемый jar - PullRequest
0 голосов
/ 24 сентября 2019

У меня есть следующая конфигурация maven

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <release>${java.version}</release>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>${main.class}</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <classpathLayoutType>repository</classpathLayoutType>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>./lib/*.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

assembly.xml:

<assembly>
    <id>assembly</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <includes>
                <include>com.relax-gaming.casinoworld:casinoworld</include>
            </includes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
            <excludes>
                <exclude>com.relax-gaming.casinoworld:casinoworld</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

Содержимое моего манифеста:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: username
Build-Jdk: 11
Specification-Title: project title
Specification-Version: 0.0.1-SNAPSHOT
Implementation-Title: Project title
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: groupid
Main-Class: path.to.main.class
Class-Path: ./lib/*.jar

Моя банка связанаследующее:

-my.code.package (from src/main/java)
+ META-INF
- MANIFEST.MF  (where the above configuration is)
+ lib
- dep1.jar (all the dependencies as jar files)
-my.resources.file (from src/main/resources)

Когда я запускаю файл, используя java -jar, я получаю NoClassDefFound errosr, потому что библиотеки не загружаются.

Я думаю, что неправильно в моем пути к классам внутри файла манифеста,Что это должно быть?

1 Ответ

0 голосов
/ 26 сентября 2019

Итак, оказывается, ты вроде не можешь.

Мое решение было следующим:

обновите файл assembly.xml и pom.xml следующим образом:

сборка:

<assembly>
    <id>assembly</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <unpack>true</unpack>
            <excludes>
                <exclude>groupId:artifactIdOfResourceIWant</exclude>
            </excludes>
        </dependencySet>
        <dependencySet>
            <outputDirectory>lib-packed</outputDirectory>
            <unpack>false</unpack>
            <includes>
                <include>groupId:artifactIdOfResourceIWant</include>
            </includes>
        </dependencySet>
    </dependencySets>
</assembly>

пом:

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <source>${maven.compiler.source}</source>
                        <target>${maven.compiler.target}</target>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

По сути, это обычно распаковывает все зависимости, но оставляет конкретную, которая вам нужна, чтобы не распаковывать ее как файл jar внутри файла jar.Это снижает сложность только до тех банок, которые вам нужно распаковать.

Теперь проблема с этим остается тем, что Class-Path может использовать только синтаксис dir / * для внешней загрузки всех банок, и его нельзя использовать для загрузкиjarfile (даже с определенными именами). Поэтому, чтобы оставить jar в комплекте и автоматически загрузить его, мне нужно изменить мой код, чтобы иметь специальный загрузчик классов, который читает этот файл.

...