2

My problem is about resources to be included in the jar file when doing release with maven.

I am using maven to build my project and when I run

$ mvn package

resources are included in the output jar. But when I run

$ mvn release:prepare

$ mvn release:perform

these resources are not included in the release jar.

Resources are located in maven default directory, src/main/resources, but they are outside of source control (ignored via .gitignore). From the output made by maven I realize that maven does git checkout to different folder and resources are not copied to that folder. This is why maven release plugin doesn't package it to release jar.

My question is what is the best way to deal with such a case?

  1. Make a symlink for resources folder during release process? how?

  2. Copy resources? how?

  3. Or putting resources into SCM is the only way to make them available in released package?

I prepared small testcase for this:

#!/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
4

3 回答 3

0

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

于 2014-01-05T19:03:49.557 回答
0

资源必须在您的 SCM 中,因为它们是您项目的一部分。否则,您的项目将无法使用这些资源。因此,您不要将它们置于 src/main/resources 作为其余项目的版本控制下,因此它们永远不会被打包到 jar 中。

于 2012-04-30T08:50:49.863 回答
0

如果您不将资源放入 SCM,则无法将它们作为该项目的一部分发布,正如其他人所提到的。

您可以尝试将资源移动到单独的项目中,并使用maven-remote-resources-plugin:bundle它来构建资源包 (jar)。每当您更改该项目中的资源时,您都需要手动修改 POM 中的版本号并执行 amvn deploy以将更改放入您的工件存储库中。

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

于 2012-04-30T14:54:13.703 回答