我的問題是關於在使用maven進行發佈時將資源包含在jar文件中。Maven在釋放時不會複製未跟蹤的資源
我使用Maven來構建我的項目,當我運行
$ mvn package
資源包含在輸出罐子。但是當我運行時
$ mvn release:prepare
$ mvn release:perform
這些資源不包含在發佈jar中。
資源位於Maven的默認目錄,src/main/resources
,但他們是外源控制(通過.gitignore
忽略)的。 從maven的輸出我意識到,maven做git簽出到不同的文件夾和資源不復制到該文件夾。 這就是爲什麼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中嗎?你意識到如果你不能單純從源頭上構建項目,它會使可重複構建變得困難? – artbristol
你能解釋爲什麼你的資源不在版本控制下的src/main/resources中嗎? – khmarbaise
資源不跟蹤SCM的原因是,它們對於特定項目來說是大的和外部的。換句話說,這些資源的變化是從外部追蹤的。 – sancoder