简体   繁体   English

将Maven2复制资源到构建目录,但不将它们捆绑在JAR中

[英]Having Maven2 copy resources to the build directory, but NOT bundle them in the JAR

I've started a new Maven project in NetBeans, accepting all the defaults. 我在NetBeans中启动了一个新的Maven项目,接受了所有默认设置。 The POM, with all the JAR dependencies stripped out, is cut-n-pasted at the bottom of this question. 将所有JAR依赖项剥离出来的POM在此问题的底部被剪切粘贴。

The application reads in various properties files (eg logging and config). 应用程序读入各种属性文件(例如,日志记录和配置)。 It also reads in external resources such as fonts, images, and sounds. 它还可读取字体,图像和声音等外部资源。 I do NOT want all these resources to be bundled up into the JAR file. 我不希望将所有这些资源捆绑到JAR文件中。 Instead, I plan to deploy them in subdirectories beneath the directory where the JAR is deployed. 相反,我计划将它们部署在部署JAR的目录下的子目录中。

A simplified view of the project's directory structure looks like this: 项目目录结构的简化视图如下所示:

-src
   |---main
           |---java
                   |---com.mypackage, etc
           |---resources
                        |---conf
                        |---fonts
                        |---images
                        |---sounds
+target

What I would like to have after a clean build would look like this: 我想这样一个干净的构建是这样以后有:

+src
-target
       |---myproject-1.0.0.jar (compiled contents of "src/main/java" ONLY)
       |---conf
       |---fonts
       |---images
       |---sounds

However, when I do a "clean-and-build" or an "exec" through NetBeans (or the command-line for that matter)... what I'm actually getting looks like this: 但是,当我通过NetBeans(或命令行)执行“清理并构建”或“执行”时......我实际得到的内容如下所示:

+src
-target
       |---classes
                  |---("src/main/java" and "src/main/resources" slammed together)
       |---myproject-1.0.0.jar (the "classes" subdirectory JAR'ed up)

Can someone point me in the right direction for getting that first result rather than the second? 有人能指出我正确的方向获得第一个结果而不是第二个结果吗? I apologize if this is a silly question (I'm a Maven rookie), or if I overlooked a previously-asked duplicate. 如果这是一个愚蠢的问题(我是Maven新手),或者如果我忽略了以前要求的副本,我道歉。 However, from the searching I've done on Stack Overflow... it looks like all the duplicate questions try to go the other way! 但是,从我在Stack Overflow上的搜索开始......看起来所有重复的问题都试图走向另一个方向! (ie get resources into a JAR rather than keep them out ) (即获得资源 JAR,而不是让他们出去

pom.xml: pom.xml中:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>steveperkins</groupId>
    <artifactId>myproject</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>My Project</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.4</source>
                    <target>1.4</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
    ...

Although the proposed solutions would work they basically work around the maven conventions. 虽然提出的解决方案可行,但它们基本上围绕着maven惯例。 A better alternative would be to filter out the resources so they are not included in the jar but still available as resources while working in the IDE. 一个更好的选择是过滤掉资源,这样它们就不会包含在jar中,但在IDE中工作时仍然可以作为资源使用。 In the pom it should look like this: 在pom中它应该是这样的:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>/conf/**</exclude>
                            <exclude>/fonts/**</exclude>
                            <exclude>/images/**</exclude>
                            <exclude>/sounds/**</exclude>
                        </excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
   </plugins>
</build>

This would effectively exclude them from the jar without any workarounds. 这将有效地将它们从罐子中排除而没有任何变通方法。

Here is the doc page for the jar plugin. 是jar插件的doc页面。

Though the above will answer your question may I suggest some additional possibility that could help you in your endeavour. 虽然以上内容将回答您的问题,但我可能会建议一些额外的可能性,以帮助您的努力。 As a second step, to still make these resources available you could package your project using the assembly plugin. 作为第二步,仍然可以使这些资源可用,您可以使用程序集插件打包您的项目。 this would allow you to create a zip file and place all the files, resources and jar, in an appropriate location so that when the zip is unpacked everything just falls into place. 这将允许您创建一个zip文件并将所有文件,资源和jar放在适当的位置,以便在解压缩zip时,所有内容都会到位。

If this project is part of a larger work you can still use the assembly plugin for each where you would have this situation and in the main project you could extract and reassemble them in a larger zip including all the necessary artifacts. 如果这个项目是一个大型工作的一部分你仍然可以使用程序集插件,你可能会遇到这种情况,在主项目中你可以提取并重新组装它们在一个更大的zip包括所有必要的工件。

Lastly I suggest you leave the directory structure under target as-is. 最后,我建议您将目录结构保留在目标下。 If you customize it it would be preferable to do it through the Maven variables so that the changes percolate to the other plugins. 如果你自定义它,最好通过Maven变量来完成它,以便更改渗透到其他插件。 If you manually remove and rename stuff once Maven has gone through you may run into problems later. 如果您在Maven完成后手动删除并重命名内容,则可能会在以后遇到问题。 Normally the Maven jar plugin should be able to just get it right if you configure it the way you want so you have no needs to worry about what comes under target. 通常情况下,如果您按照自己的方式配置它,Maven jar插件应该能够正确使用它,这样您就不必担心目标是什么了。 Personally I use Eclipse and the pusign is pretty good at getting the IDE and Maven config in sync. 就个人而言,我使用Eclipse,并且pusign非常适合让IDE和Maven配置同步。 For NetBeans I would suspect this would also be the case. 对于NetBeans,我怀疑这也是如此。 If not the best approach would be to configure your project in NetBeans to use target/classes as a target folder for built artifacts and target/test-classes for stuff built from src/test/java. 如果不是,最好的方法是在NetBeans中配置项目,将目标/类用作构建工件的目标文件夹,并将目标/测试类用于从src / test / java构建的东西。

Personally, I would not use the default location of resources but an "extra" location and configure the resources plugin to copy them where you want from there: 就个人而言,我不会使用资源的默认位置,而是使用“额外”位置,并配置资源插件将它们从那里复制到您想要的位置:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>
    </plugins>
    ...
  </build>
  ...
</project>

If you insist with using the default location ( src/main/resources ), I think you'll have to configure some exclusions (see below) to avoid resources getting copied by default and then use the same approach as above. 如果你坚持使用默认位置( src/main/resources ),我认为你必须配置一些排除(见下文)以避免资源被默认复制,然后使用与上面相同的方法。

Another option would be to use the AntRun maven plugin and Ant to move files but this is not really the maven way so I won't detail it. 另一种选择是使用AntRun maven插件和Ant来移动文件,但这不是真正的maven方式所以我不会详细说明。

Resources 资源

您可以声明特殊的资源执行:copy-resources目标。

Eugene is on the right track but there's a better way to make this work. Eugene走在正确的轨道上,但有更好的方法来完成这项工作。

It should look something like this: 它应该看起来像这样:

<build>
  <outputDirectory>target/${artifactId}-${version}</outputDirectory>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.3.1</version>
      <configuration>
        <classesDirectory>${project.build.outputDirectory}</classesDirectory>
        <outputDirector>target</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
  <resources>
    <resource>
      <directory>src/main/resources/conf</directory>
      <targetPath>../conf</targetPath>
    </resource>
    <resource>
      <directory>src/main/resources/ANOTHER_PATH</directory>
      <targetPath>../ANOTHER_PATH</targetPath>
    </resource>
  </resources>
</build>

You won't be able to get rid of the 'classes' directory, but you'll be able to give it a different name that shouldn't interfere with NetBeans. 你将无法摆脱'classes'目录,但你将能够给它一个不应该干扰NetBeans的不同名称。

You can find more about the <outputDirectory> element here . 您可以在此处找到有关<outputDirectory>元素的更多信息。

You can find more about the jar plugin here . 你可以在这里找到关于jar插件的更多信息。

You can find more about the <resource> element here . 您可以在此处找到有关<resource>元素的更多信息。

As a side note, you may want to consider running Maven under a 1.6 JDK and fork the maven-compiler-plugin to use your 1.4 JDK for compiling. 作为旁注,您可能需要考虑在1.6 JDK下运行Maven并分叉maven-compiler-plugin以使用1.4 JDK进行编译。 You can find out more about this here . 你可以在这里找到更多相关信息。 That should give you a boost to your compile time. 这应该会促进你的编译时间。 You can also tell surefire when running test cases to use the 1.4 JDK for execution as well. 您还可以在运行测试用例时告诉surefire以使用1.4 JDK执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM