我的问题是关于使用 maven 进行发布时要包含在 jar 文件中的资源。
我正在使用 Maven 来构建我的项目,当我运行时
$ mvn package
资源包含在输出 jar 中。但当我跑步时
$ mvn release:prepare
$ mvn release:perform
这些资源不包含在发布 jar 中。
资源位于maven默认目录中,src/main/resources
,但它们不在源代码控制范围内(通过忽略.gitignore
)。
从 maven 的输出中,我意识到 maven 会 git checkout 到不同的文件夹,并且资源不会复制到该文件夹。
这就是为什么maven发布插件不将其打包为发布jar的原因。
我的问题是处理这种情况的最佳方法是什么?
在发布过程中为资源文件夹创建符号链接?如何?
复制资源?如何?
或者将资源放入 SCM 是使它们在发布的包中可用的唯一方法?
我为此准备了小测试用例:
#!/bin/sh
# here is pom.xml
cat >pom.xml <<EOF
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.domain</groupId>
<artifactId>artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>artifact</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<scm>
<developerConnection>scm:git:file://$(pwd)</developerConnection>
</scm>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Example Internal Repository</name>
<url>file://$(pwd)/deploy</url>
</repository>
</distributionManagement>
</project>
EOF
#here is src/main/java/Main.java
mkdir src
mkdir src/main
mkdir src/main/java
mkdir src/main/resources
cat >src/main/java/Main.java <<EOF
import java.io.InputStream;
public class Main
{
public static void main(String args[])
{
boolean ok = false;
InputStream is = new Main().getClass().getResourceAsStream("/some_file_outside_scm");
ok = is != null;
System.out.println("ok=" + ok);
}
}
EOF
#some data in src/main/resources/some_file_outside_scm
cat >src/main/resources/some_file_outside_scm <<EOF
123
456
EOF
#file src/main/resources/.gitignore
cat >src/main/resources/.gitignore <<EOF
some_file_outside_scm
EOF
#.gitignore in the project root
cat >.gitignore <<EOF
target
deploy
EOF
git init .
git add pom.xml
git add src/main/java/Main.java
git commit -m "first commit"
#The test case can be tested with these commands
mvn package
echo !!!!!!!!!!!!!!!!!
echo contents of the packaged jar
jar tf target/artifact-0.0.1-SNAPSHOT.jar
#The file 'some_file_outside_scm' is packaged into jar.
echo !!!!!!!!!!!!!!!!!
echo after mvn package
java -cp target/artifact-0.0.1-SNAPSHOT.jar Main
# ok=true
echo !!!!!!!!!!!!!!!!!
mvn release:prepare
mvn release:perform
echo !!!!!!!!!!!!!!!!!
echo but after mvn release
java -cp deploy/com/domain/artifact/0.0.1/artifact-0.0.1.jar Main
# ok=false
#the file is not included in the release jar
echo !!!!!!!!!!!!!!!!!
echo contents of the release jar
jar tf deploy/com/domain/artifact/0.0.1/artifact-0.0.1.jar
如果您不将资源放入 SCM,则无法将它们作为该项目的一部分发布,正如其他人提到的那样。
您可以尝试将资源移至单独的项目中并使用maven-remote-resources-plugin:bundle http://maven.apache.org/plugins/maven-remote-resources-plugin/构建资源包(jar)。每当您更改该项目中的资源时,您都需要手动更改 POM 中的版本号并执行mvn deploy
将更改放入您的工件存储库中。
然后,在您当前的项目中,您使用maven-remote-resources-plugin:process
在构建工件之前检索资源并将它们放在工件中的正确位置。process
绑定到generate-resources
默认阶段;调整此以满足您的需要。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)