Includere il path delle dipendenze in Maven

Mattepuffo's logo
Includere il path delle dipendenze in Maven

Includere il path delle dipendenze in Maven

Una volta finito di creare un programma in Java usando Maven, ho provato a lanciarlo fuori dall'IDE ed ho riscontrato un problema con le dipendenze:

java.lang.NoClassDefFoundError: org/apache/tika/exception/TikaException

Classico problema perchè il Manifest non trova le librerie richieste dal programma.

In sostanza bisogna modificare il Manifest per includere la directory con le librerie.

Per farlo basta aggiungere due plugin al pom.xml:

        <plugins>
            ..................................
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.cimoda.rest.Main</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            
        </plugins>

Mi raccomando sul plugin maven-jar-plugin: la versione 3.x ha un baco, e non funziona.

Potete guardare a questa discussione per un resoconto più preciso.

Enjoy!


Condividi

Commentami!