【问题标题】:Maven Build using WebApplicationInitializer instead of web.xmlMaven 使用 WebApplicationInitializer 而不是 web.xml 构建
【发布时间】:2015-02-18 02:33:08
【问题描述】:

我遇到了一些问题。我正在使用所有基于 Java 的配置,减去 logback.xml,因为我没有找到一个很好的例子来说明如何在没有 XML 的情况下进行配置。

反正我的问题是什么时候做

mvn clean build

因为找不到 web.xml 而失败。这是一个问题,因为我使用的是 QueryDSL,它有一个 Maven 插件来生成它需要的类。所以我需要能够在没有 web.xml 的情况下进行 Maven 构建,或者我需要能够在没有 Maven 的情况下生成 QueryDSL 元类。

<build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <wtpversion>2.0</wtpversion>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <finalName>igdb</finalName>
    </build>

我的 web.xml 已被此代码替换。

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class ApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {

        /**
         * If no active profile is set via -Dspring.profiles.active then the application
         * will default to development mode
         */
         container.setInitParameter("spring.profiles.default", "dev");

        /**
         * create the root Spring application context
         */
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.setDisplayName("IGDb");
        rootContext.register(AppConfig.class);

        /**
         * manage the lifecycle of the root application context
         */
        container.addListener(new ContextLoaderListener(rootContext));

        /**
         * register and map the dispatcher servlet
         */
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

【问题讨论】:

    标签: java maven querydsl spring-java-config


    【解决方案1】:

    试试这个:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-war-plugin</artifactId>
     <version>2.1.1</version>
     <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>
     </configuration>
    </plugin>
    

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 2011-11-04
      • 2023-04-10
      • 2014-10-12
      • 1970-01-01
      • 2015-04-28
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多