Maven WebApp META-INF context.xml

16

我正在使用Maven 3,并尝试在webapp文件夹下添加META-INF文件夹。因此,我正在尝试执行以下操作:

src
 main
  webapp
   META-INF
    context.xml
   WEB-INF

以下是我的POM文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.data</groupId>
<artifactId>Java-WebApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Java-Web Application</name>

<!-- Shared version number properties-->
<properties>
<org.springframework.version>3.0.6.RELEASE</org.springframework.version>
</properties>

<dependencies>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>
 </dependencies>
<build>
<finalName>data</finalName>
</build>

<parent>
<groupId>com.data</groupId>
<artifactId>Java-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

在src/main/resources下,我添加了一个META-INF\ context.xml文件。当使用mvn package打包WAR文件时,其结构看起来像这样:

data
 webapp
  META-INF
  WEB-INF
  index.jsp

可以看到WEB-INF下的相关文件。但是META-INF文件夹为空。我的默认Maven将在WEB-INF/classes下添加资源。

我希望特别地拥有:

data
 webapp
  META-INF
   context.xml
  WEB-INF

这怎么可能?我已经尝试了其他方法,但仍然无法正常工作。context.xml 包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/data1"/>

我尝试将META-INF文件夹从src\main\resources目录中删除,并直接放在webapp\META-INF目录下。虽然context.xml被显示了出来,但是当部署到Tomcat中后,我定义的context根目录并不起作用。


不确定是否有效,但您可以尝试将META-INF文件夹移动到resources/META-INF - Lorenz
你的初始问题解决了吗? - Evgeniy Dorofeev
不,问题仍然存在。 - user1646481
但是 context.xml 现在会被打包到 war 包中吗? - Evgeniy Dorofeev
1
是的,它可以通过删除src\main\resources并将META-INF文件夹直接添加到webapp下来实现。但是context-root仍然无法工作。 - user1646481
2个回答

24

将 META-INF 文件夹放置在 src/main/resources 目录下。

将以下内容添加到您的 pom.xml 中... 抱歉之前我忘记了;

<build>
<plugins>
    <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>


                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>

                <webResources>
                    <resource>
                        <!-- this is relative to the pom.xml directory -->
                        <directory>${project.basedir}/src/main/resources
                        </directory>

                    </resource>
                </webResources>



                <warName>mywar</warName>
            </configuration>
        </plugin>
</plugins>
</build>

网络资源标签是重点......希望这可以帮到你。


这个可以运行,但是在Tomcat中加载Web应用程序时,上下文根无法工作。 - user1646481
这个链接可能对你有所帮助:https://dev59.com/eXE85IYBdhLWcg3w03Hz。 - dinukadev
如果可能的话,这个答案可以通过仅包含相关代码和上下文而不是整个<build />标签来改进。 - Madbreaks

-2
请使用标签。上面的答案将src/main/resources中的所有内容复制到您的Web应用程序的根目录,这可能不是您想要做的,也不是您想要遵循的示例。

这个答案不够清晰,需要进一步解释。 - Madbreaks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接