为什么Maven JAR插件不包含某些资源? [英] Why is the Maven JAR plugin not including some resources?

查看:95
本文介绍了为什么Maven JAR插件不包含某些资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个企业应用程序,正在将其从Ant构建转换为Maven.它几乎完全转换了;这是我需要修复的最后一件事.该应用程序打包为EAR文件,其中包含两个WAR,并具有一个JAR模块,该模块提供了该应用程序的所有核心功能.

I have an enterprise application which I am in the process of converting from an Ant build to Maven. It's almost completely converted; this is the very last thing I need to fix. The application is packaged as an EAR file which contains two WARs and has a JAR module which provides all of the core functionality of the application.

我正在使用Freemarker模板库来生成应用程序发送的自动电子邮件的消息正文. Freemarker需要其* .ftl模板文件位于类路径中,并且由于这是不是特定于一个WAR或另一个WAR的核心应用程序功能,因此它必须位于JAR中.

I'm using the Freemarker templating library to generate, among other things, message bodies for automatic emails sent by the application. Freemarker needs its *.ftl template files to be on the classpath, and since this is core application functionality not specific to one WAR or the other, it needs to be in the JAR.

定义JAR的Maven模块具有以下POM:

The Maven module which defines the JAR has the following POM:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <parent>
        <relativePath>../../pom.xml</relativePath>
        <groupId>com.company.project</groupId>
        <artifactId>projectName</artifactId>
        <version>1.8.0</version>
    </parent>

    <artifactId>core</artifactId>
    <packaging>jar</packaging>
    <name>Core Application</name>

    <profiles>
       <!-- snip -->
    </profiles>

    <dependencies>
       <!-- snip -->
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.ftl</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- snip -->
        </plugins>
    </build>
</project>

*.ftl文件位于src/main/resources/template/中,其中一些位于template/的子目录中. src/main/resources中还有其他文件-一些.properties和一些.xml,一些在根目录下,有些在目录结构下.

The *.ftl files are located at src/main/resources/template/, with some in a subdirectory within template/. There are other files within src/main/resources -- some .properties and some .xml, some at the root and some under a directory structure.

当我在此模块(或父模块)上运行程序包阶段时,在构建过程中创建的target/classes目录包含模板目录,而模板目录又包含所有* .ftl,*.xml以及具有适当目录结构的* .properties文件.如果我手动将该目录设置为JAR,则一切正常.

When I run the package phase on this module (or on the parent), the target/classes directory created as part of the build process contains the template directory, which in turn contains all of the *.ftl, *.xml, and *.properties files with an appropriate directory structure. If I JAR this directory up manually, everything works perfectly.

这是很奇怪的地方,我迷路了:当maven-jar-plugin创建JAR时,它包含XML和属性文件,但是JAR完全没有模板目录,并且找不到其内容.

Here's where this gets weird and I get lost: when maven-jar-plugin creates the JAR, it includes the XML and properties files, but the template directory is completely absent from the JAR and its contents are nowhere to be found.

正如您在上面看到的,我尝试显式包括**/*.ftl.没关系;我可以排除整个"includes"标签,然后得到完全相同的行为.

As you can see above, I tried explicitly including **/*.ftl. It doesn't make a difference; I can exclude the entire "includes" tag and I get the exact same behavior.

我正在使用Maven 3.0.5和maven-jar-plugin 2.4.

I'm using Maven 3.0.5 and maven-jar-plugin 2.4.

推荐答案

提交此问题后,我想出了正确的答案.

I figured out the answer RIGHT after submitting this question.

在父POM中,我有以下内容:

In a parent POM, I had the following:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <includes>
            <include>**/*.class</include>
            <include>**/*.jdo</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
    </configuration>
</plugin>

我将**/*.ftl添加到了包含列表中,现在它可以正常工作了.

I added **/*.ftl to the list of includes and now it's working.

更好的是,我完全删除了配置标签,并且它仍在工作.我认为这是我发现类路径上需要的.properties文件和其他内容之前应该在src/main/resources中而不是src/main/java中的残余.

Better yet, I removed the configuration tag entirely, and it's still working. I think it was a remnant from before I figured out that the .properties files and other things I needed on the classpath needed to be in src/main/resources and not src/main/java.

这篇关于为什么Maven JAR插件不包含某些资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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