java - Maven:maven 构建中的类下缺少文件

标签 java eclipse maven api maven-tomcat-plugin

请耐心等待,我是 Maven 新手。 所以,我用以下结构 checkin 了这个项目-

-WebContent
-src
-pom.xml

当我通过命令行构建此项目时,使用命令-

mvn clean
mvn package

我得到了一个包含 .war 文件的目标文件夹,我将其部署到 tomcat 服务器。我的项目 UI 已显示,但没有调用任何 API。 但是当我将同一个项目导入 Eclipse IDE 并构建它时,我注意到有一组额外的文件和文件夹 -

  • build
  • .classpath
  • ImportedClasses
  • .project
  • .settings

以及目标目录中的类下的一组额外的 .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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>GenericApp</groupId>
<artifactId>GenericApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
                   <groupId>javax.servlet</groupId>
                   <artifactId>javax.servlet-api</artifactId>
                   <version>3.0.1</version>
                   <scope>provided</scope>
           </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency><!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>2.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.4</version>
    </dependency>


    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20171018</version>
    </dependency>


</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

为什么两个构建都会从同一组文件中产生不同的结果? 我该如何解决和理解这个问题?

最佳答案

默认情况下,maven包通过编译打包并在war文件中添加默认资源来添加默认的src。

基于 Maven 的 Web 应用程序中资源的默认位置是: src/主/资源 如果您的资源位于其他位置,则不会将其添加到 war 文件中。

如果您希望包含其他资源,例如任何 .xml 文件或图像等,您可以在 pom.xml 中明确提及。

示例:在您的构建标记内添加以下内容:

<resources>
 <resource>
   <directory>[your folder here]</directory>
 </resource>

以上指令会将资源添加到war文件夹根目录的WEB-INF中。 如果您想将它们添加到特定位置,那么您可以添加

<resources>
 <resource>
   <directory>[your folder here]</directory>
   <targetPath>[you target path in WEB_INF/classes</targetPath>
 </resource>

示例:

 <resource>
    <directory>src/com/ekagga/ga/config/</directory>
    <targetPath>com/ekagga/ga/config</targetPath>
    <filtering>true</filtering>
  </resource>
</resources>

默认它会添加所有文件,如 .java 等,您可能需要添加过滤器来删除此类文件。

关于java - Maven:maven 构建中的类下缺少文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50921582/

相关文章:

java - JAVA如何同时显示2个if语句

Java求助Decimal format cannot be resolved to a type

java - 安卓错误: Unable To Start Activity

java - 无法检索 'this' 的正确封闭实例

maven - Gradle 可以将不同的 Artifact 上传到不同的 Maven 存储库吗?

java - 有没有办法用唯一的配置文件编译类并以其他方式排除它?

java - java 的 strace 或 procmon 等效项

java - 求和文本文件中整数的最快方法

java - 自动生成标题

java - 如何解决 Maven Invoker API 警告 : Maven will be executed in interactive mode, 但没有配置输入流