将FXML文件保留在/ src / main / java中

时间:2015-04-15 15:49:12

标签: java maven

每当我将FXML文件放入/src/main/java目录中时,似乎它们在编译期间不会被包含在最终的OSGi jar中。我想Maven会从那里删除它们,因为它认为FXML文件应该只存在于/src/main/resouces目录中。有没有办法阻止Maven这样做(只是留在那里)?

编辑1
我现在找到的唯一解决方案是:

<build>
<plugins>
....
 <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>generate-sources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
            <outputDirectory>${basedir}/target/classes/</outputDirectory>
             <resources>
            <resource>
              <directory>src/main/java</directory>
               <includes>
                <include>**/*.fxml</include>
                <include>**/*.css</include>
              </includes>
            </resource>
          </resources>           
            </configuration>            
          </execution>
         </executions>
      </plugin> 
</plugins>
</build>

但是,据我所知,在<build>...</build>节点中还有另一种更短的方法,即即。,而不使用额外的插件。我该如何使用更短的方法?

1 个答案:

答案 0 :(得分:2)

默认情况下,src/main/resources是资源文件。 您可以使用maven resources插件来覆盖此行为。

这是一个例子。在pom.xml

...
<build>
    <resources>
        <resource>
        <targetPath>com/company/projectname</targetPath>
            <directory>src/main/java/com/company/projectname</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

其中<directory>是源包,其中包含资源文件(在您的情况下为xml文件),<targetPath>是您希望资源驻留的目标。如果您可以在jar的根路径中使用这些资源,则可以省略<targetPath>