2012-04-30 140 views
2

我的问题是关于在使用maven进行发布时将资源包含在jar文件中。Maven在释放时不会复制未跟踪的资源

我使用Maven来构建我的项目,当我运行

$ mvn package 

资源包含在输出罐子。但是当我运行时

$ mvn release:prepare 

$ mvn release:perform 

这些资源不包含在发布jar中。

资源位于Maven的默认目录,src/main/resources,但他们是外源控制(通过.gitignore忽略)的。 从maven的输出我意识到,maven做git签出到不同的文件夹和资源不复制到该文件夹​​。 这就是为什么Maven发布插件不打包它释放jar。

我的问题是处理这种情况的最佳方法是什么?

  1. 在发布过程中创建资源文件夹的符号链接?怎么样?

  2. 复制资源?怎么样?

  3. 或者将资源投入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 
+0

这些资源不在您想要发布的SCM中吗?你意识到如果你不能单纯从源头上构建项目,它会使可重复构建变得困难? – artbristol

+0

你能解释为什么你的资源不在版本控制下的src/main/resources中吗? – khmarbaise

+0

资源不跟踪SCM的原因是,它们对于特定项目来说是大的和外部的。换句话说,这些资源的变化是从外部追踪的。 – sancoder

回答

0

如其他人所述,如果您不将资源投入SCM,则无法将其作为此项目的一部分发布。

您可能会尝试将资源移动到单独的项目中,并使用maven-remote-resources-plugin:bundle来构建资源包(jar)。无论何时您更改该项目中的资源,您都需要手动对POM中的版本号进行冲击,并执行mvn deploy以将更改导入您的工件回购。

然后,在您当前的项目中,您使用maven-remote-resources-plugin:process来检索资源,并在构建它们之前将它们放入工件中的适当位置。 process默认绑定到generate-resources阶段;调整此以满足您的需求。

+0

好吧,如果只有一个选项,我会接受这个答案。感谢您指出资源可能位于单独的项目中。 – sancoder

0

资源必须在你的供应链管理,因为他们是你的项目的一部分。否则,您的项目无法使用这些资源。所以你不要把它们在版本控制下放到src/main/resources中作为其余的项目,这样它们就永远不会被打包到jar中。

0

发布插件在release:perform期间激活了配置文件release-perform。您可以使用该配置文件的资源插件来复制您的资源。