S
S
Saveli Tomak2017-03-07 20:37:35
Java
Saveli Tomak, 2017-03-07 20:37:35

How to specify Class-Path in MANIFEST.MF maven?

Hello everyone, in general, I'm trying to build a spring application using maven in a jar. The local repository is in ~/.m2/repository.
MANIFEST.MF:

Manifest-Version: 1.0.0
Created-By: Saveli Tomak
Main-Class: App
Class-Path: /home/saveli/.m2/repository

pow.xml:
...
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
    </dependencies>
...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>

                <configuration>
                    <archive>
                        <manifestFile>${dir.resources}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
...

I compile with the mvn package command, the jar'nick received at the output of Spring does not include classes, after launch I get
java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext

In Java, I am new lvl and don’t know what to do about it, in the MANIFEST.MF examples that I found on the Internet, the Class-Path indicated the paths to the Spring jars themselves, but I think this solution is not very universal.
Thank you all in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2017-03-07
@aresouji

<plugins>

        <!--Настройка компиляции проекта-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>

                <configuration>

                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


            <!--Копирование зависимотей в папку lib/-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <configuration>

                    <!--Все зависимости которые нужны для работы твоего приложения будут лежать здесь-->
                    <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                    <overWriteReleases>true</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>

                </configuration>
                <executions>
                    <execution>

                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>

                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>

                <configuration>
                    <archive>

                        <!--Если надо конфигурировать манифест, то здесь-->
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <!--путь к зависимостям относительно запускаемого jar файла-->
                            <!--ВСЕ ЗАВИСИМОСТИ ДОЛЖНЫ БУДУТ ПОСТАВЛЯТЬСЯ С ТВОИМ jar приложением. -->
                            <classpathPrefix>lib/</classpathPrefix>
                            <classpathLayoutType>simple</classpathLayoutType>

                            <mainClass>main.Main</mainClass>
                        </manifest>
                        <!--<manifestEntries>-->
                        <!--<Rsrc-Class-Path>${}</Rsrc-Class-Path>-->
                        <!--</manifestEntries>-->

                    </archive>
                </configuration>
            </plugin>

        </plugins>

Maven will build the jar file, edit the manifest. The resulting jar will be in the target folder - I hope you know what it is. There will also be a lib/ folder with all dependencies (other jar files). If you take the resulting jar and this folder, and put it together in another folder, you get a full-fledged distribution. But this is a different plugin. You'll figure it out over time.
In your version, in general, everything is initially not true. The manifest must contain all the used jar files. Try not to go into the manifest at all. And you only have /home/saveli/.m2/repository - there are a bunch of folders with a bunch of jar versions, so it doesn't find anything.
The third plugin can be removed altogether, but then all jar files from the target / lib / folder should lie next to the resulting jar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question