Stellen Sie im Maven Dependency Plugin Folgendes ein.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includeArtifactIds>ajd4jp,jdom</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Wenn mvn package mit dieser Einstellung ausgeführt wird, werden Klassendateien usw. aus abhängigen JAR-Dateien wie Bibliotheken erweitert und in das in outputDirectory angegebene Verzeichnis kopiert, bevor eine JAR-Datei erstellt wird. Beim Erstellen einer JAR-Datei werden auch die Klassendateien in den abhängigen JAR-Dateien zu einer JAR-Datei zusammengefasst.
Bereiten Sie eine pom.xml-Datei vor.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>sample</name>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Apache Maven Dependency Plugin -->
<!-- https://maven.apache.org/plugins/maven-dependency-plugin/ -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includeArtifactIds>ajd4jp,jdom</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ajd4jp</groupId>
<artifactId>ajd4jp</artifactId>
<version>1.4.6.2019</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ajd4jp-1.4.6.2019.jar</systemPath>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jdom-1.1.3.jar</systemPath>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Erstellen Sie eine JAR-Datei mit dem Befehl mvn package.
$ mvn package
Wenn Sie die erstellte Datei target / sample.jar erweitern, sehen Sie, dass sie die Klassendatei enthält, die in der JAR-Datei der angegebenen Bibliothek enthalten war.
$ unzip target/sample.jar
Archive: target/sample.jar
creating: META-INF/
inflating: META-INF/MANIFEST.MF
creating: orrery/
creating: iso/
creating: org/
creating: org/jdom/
creating: org/jdom/xpath/
creating: org/jdom/input/
creating: org/jdom/output/
creating: org/jdom/adapters/
creating: org/jdom/filter/
creating: org/jdom/transform/
creating: ajd4jp/
creating: ajd4jp/orrery/
creating: ajd4jp/orrery/tool/
creating: ajd4jp/iso/
creating: ajd4jp/util/
creating: ajd4jp/format/
creating: format/
creating: com/
creating: com/example/
inflating: orrery/package-info.class
inflating: Copyright.txt
inflating: iso/package-info.class
inflating: org/jdom/IllegalAddException.class
inflating: org/jdom/DefaultJDOMFactory.class
inflating: org/jdom/EntityRef.class
(Unterlassung)
Durch die Einführung des Maven Assembly Plugins können nicht nur JAR-Dateien mit Systemumfang, sondern auch abhängige JAR-Dateien im Repository zusammengefasst werden.
Bereiten Sie eine pom.xml-Datei vor.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>sample</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>sample</name>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Apache Maven Dependency Plugin -->
<!-- https://maven.apache.org/plugins/maven-dependency-plugin/ -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-dependency-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includeArtifactIds>ajd4jp,jdom</includeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<!-- Apache Maven Assembly Plugin -->
<!-- http://maven.apache.org/plugins/maven-assembly-plugin/ -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ajd4jp</groupId>
<artifactId>ajd4jp</artifactId>
<version>1.4.6.2019</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/ajd4jp-1.4.6.2019.jar</systemPath>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jdom-1.1.3.jar</systemPath>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.7</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Erstellen Sie eine JAR-Datei mit dem Befehl mvn package.
$ mvn package
Wenn Sie die erstellte Datei target / sample-jar-with-dependencies.jar erweitern, sehen Sie, dass sie die Klassendateien enthält, die in der JAR-Datei der im Systembereich und im Kompilierungsbereich angegebenen Bibliothek enthalten sind.
$ unzip target/sample-jar-with-dependencies.jar
Archive: target/sample-jar-with-dependencies.jar
inflating: META-INF/MANIFEST.MF
creating: twitter4j/
creating: twitter4j/util/
creating: twitter4j/util/function/
creating: twitter4j/auth/
creating: twitter4j/management/
creating: twitter4j/json/
creating: twitter4j/api/
creating: twitter4j/conf/
creating: META-INF/maven/
creating: META-INF/maven/org.twitter4j/
creating: META-INF/maven/org.twitter4j/twitter4j-core/
creating: orrery/
creating: iso/
creating: org/
creating: org/jdom/
creating: org/jdom/xpath/
creating: org/jdom/input/
creating: org/jdom/output/
creating: org/jdom/adapters/
creating: org/jdom/filter/
creating: org/jdom/transform/
creating: ajd4jp/
creating: ajd4jp/orrery/
creating: ajd4jp/orrery/tool/
creating: ajd4jp/iso/
creating: ajd4jp/util/
creating: ajd4jp/format/
creating: format/
creating: com/
creating: com/example/
creating: META-INF/maven/com.example/
creating: META-INF/maven/com.example/sample/
inflating: META-INF/LICENSE.txt
inflating: twitter4j/JULLoggerFactory.class
inflating: twitter4j/SymbolEntity.class
inflating: twitter4j/MediaEntity.class
inflating: twitter4j/TwitterBase.class
inflating: twitter4j/Dispatcher.class
inflating: twitter4j/HttpClientBase.class
(Unterlassung)
Recommended Posts