Maven在释放时不会复制未跟踪的资源 [英] Maven doesn't copy untracked resources while releasing

查看:126
本文介绍了Maven在释放时不会复制未跟踪的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

p>

  $ mvn包

资源包含在输出罐中。但是,当我运行

  $ mvn发行版:准备

$ mvn发行版:执行

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



资源位于maven默认目录 src / main / resources 中,但它们不在源代码控制之列(通过 .gitignore )。
从maven的输出中,我发现maven会将git签出到不同的文件夹,并且资源不会复制到该文件夹​​中。
这就是为什么Maven发布插件不会打包发布jar。



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


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


  2. 复制资源?如何?

  3. 或者将资源放入SCM是使它们在发布的包中可用的唯一方法?


我为此准备了一个小测试用例:

 #!/ bin / sh 
#这里是pom.xml
cat> pom.xml<< EOF
< project xmlns =http://maven.apache.org/POM/4.0。 0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache .ORG / XSD /行家-4.0.0.xsd>
< modelVersion> 4.0.0< / modelVersion>

< groupId> com.domain< / groupId>
< artifactId>工件< / artifactId>
< version> 0.0.1-SNAPSHOT< / version>
<包装> jar< / packaging>

<名称>神器< /名称>
< url> http://maven.apache.org< / url>

<属性>
< project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
< / properties>

< scm>
< developerConnection> scm:git:file:// $(pwd)< / developerConnection>
< / scm>

< distributionManagement>
< repository>
< id> internal.repo< / id>
<名称>示例内部存储库< /名称>
< url> file:// $(pwd)/ deploy< / url>
< / repository>
< / distributionManagement>

< / project>
EOF

#这里是src / main / java / Main.java
mkdir src
mkdir src / main
mkdir src / main / java
mkdir src / main / resources
$ b $ 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

#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在项目根目录
cat> .gitignore< < EOF
目标
部署
EOF

git init。
git add pom.xml
git add src / main / java / Main.java
git commit -m第一次提交

#测试用例可以是用这些命令测试

mvn包
回声!!!!!!!!!!!!!!!!!
打包jar的回显内容
jar tf target / artifact-0.0.1-SNAPSHOT.jar
#文件'some_file_outside_scm'打包成jar。

回声!!!!!!!!!!!!!!!!!
在mvn包之后回显
java -cp target / artifact-0.0.1-SNAPSHOT.jar主
#ok = true
回声!!!!!!!!!! !!!!!!!

mvn发行版:准备
mvn发行版:执行

回声!!!!!!!!!!!!!!!!!
echo但在mvn发布后
java -cp deploy / com / domain / artifact / 0.0.1 / artifact-0.0.1.jar Main
#ok = false

#文件不包含在发布jar
回声!!!!!!!!!!!!!!!!!
发布jar的回显内容
jar tf deploy / com / domain / artifact / 0.0.1 / artifact-0.0.1.jar


解决方案

如果您没有将资源投入到SCM中,则不能将其作为此项目的一部分发布。



您可以尝试将资源移动到单独的项目中,并使用 maven-remote-resources-plugin:bundle 来构建一个资源包(jar)。每当您更改该项目中的资源时,您都需要手动在POM中打开版本号,然后执行 mvn deploy 以将更改导入您的工件回购。 p>

然后,在您当前的项目中,使用 maven-remote-resources-plugin:process 来检索资源并放入他们在制造之前在你的神器中的适当位置。默认情况下, process 被绑定到 generate-resources 阶段。调整这个以满足您的需求。


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

解决方案

If you are not putting the resources into SCM, you can't release them as part of this project, as mentioned by others.

You might try moving the resources into a separate project and using maven-remote-resources-plugin:bundle to build a resource bundle (jar). Any time you change a resource in that project you would need to manually bump the version number in your POM and do a mvn deploy to get the changes into your artifact repo.

Then, in your current project, you use maven-remote-resources-plugin:process to retrieve the resources and put them in the proper location in your artifact before it is built. process is bound to the generate-resources phase by default; adjust this to meet your needs.

这篇关于Maven在释放时不会复制未跟踪的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆