如何在 Maven 包中包含自定义文件夹?
How to include custom folder in Maven Package?
下面是我的项目结构:
META-INF/abc.jpg
META-INF/xyz.jpg
src/com/hcc/files.java
pom.xml
以下是pom.xml
的内容:
<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>xyz</groupId>
<artifactId>zyx</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="${basedir}/META-INF/services/abc.px" tofile="${project.build.directory}/META-INF/services/abc.px" />
<copy file="${basedir}/META-INF/services/xyz.px" tofile="${project.build.directory}/META-INF/services/xyz.px" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>abc</groupId>
<artifactId>abc</artifactId>
<version>936</version>
<scope>system</scope>
<systemPath>path</systemPath>
</dependency>
</dependencies>
</project>
当我尝试使用 mvn compile
进行编译时,它会创建一个目标文件夹。
ex - target/classes/com/hcc
如何在创建包时包含 META-INF 文件夹,即 mvn 包,以便我的 jar 文件包含目标文件夹文件和 META-INF
文件夹?
在 pom.xml
的构建部分,在 Maven war 插件声明下,您可以添加必要的配置,在其中将 Web 资源指定为目录及其输出路径:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-war-plugin.version}</version>
<configuration>
<webResources>
<resource>
<directory>META-INF</directory>
<filtering>true</filtering>
<targetPath>META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
或者直接将其作为资源添加到构建下:
<build>
....
<resources>
<resource>
<directory>META-INF</directory>
</resource>
</resources>
</build>
在我的 pom.xml 的构建部分,我又添加了一个资源标签和相应的配置,以便在打包时将自定义文件复制到 jar 中。
<resource>
<directory>META-INF</directory>
<includes>
<include>**services/**</include>
</includes>
<targetPath>META-INF</targetPath>
</resource>
下面是我的项目结构:
META-INF/abc.jpg
META-INF/xyz.jpg
src/com/hcc/files.java
pom.xml
以下是pom.xml
的内容:
<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>xyz</groupId>
<artifactId>zyx</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy file="${basedir}/META-INF/services/abc.px" tofile="${project.build.directory}/META-INF/services/abc.px" />
<copy file="${basedir}/META-INF/services/xyz.px" tofile="${project.build.directory}/META-INF/services/xyz.px" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>abc</groupId>
<artifactId>abc</artifactId>
<version>936</version>
<scope>system</scope>
<systemPath>path</systemPath>
</dependency>
</dependencies>
</project>
当我尝试使用 mvn compile
进行编译时,它会创建一个目标文件夹。
ex - target/classes/com/hcc
如何在创建包时包含 META-INF 文件夹,即 mvn 包,以便我的 jar 文件包含目标文件夹文件和 META-INF
文件夹?
在 pom.xml
的构建部分,在 Maven war 插件声明下,您可以添加必要的配置,在其中将 Web 资源指定为目录及其输出路径:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven-war-plugin.version}</version>
<configuration>
<webResources>
<resource>
<directory>META-INF</directory>
<filtering>true</filtering>
<targetPath>META-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
或者直接将其作为资源添加到构建下:
<build>
....
<resources>
<resource>
<directory>META-INF</directory>
</resource>
</resources>
</build>
在我的 pom.xml 的构建部分,我又添加了一个资源标签和相应的配置,以便在打包时将自定义文件复制到 jar 中。
<resource>
<directory>META-INF</directory>
<includes>
<include>**services/**</include>
</includes>
<targetPath>META-INF</targetPath>
</resource>