MAVEN packaging contains more resource files (src/main/java,src/main/resources)

Keywords: xml JSP Maven Java

How to include more resource files when packaging with MAVEN

First, let's look at the directory structure of the MAVENx project standard:


 

Generally, the resource files we use (all kinds of xml, properties, xsd files, etc.) are placed under src/main/resources. When maven is used to package these resource files, maven can package them into the corresponding jar or war.

 

Sometimes, for example, mybatis mapper.xml file, we used to put it together with Mapr. java, all under src/main/java, so when using Maven to package, we need to modify the pom.xml file to package mapper.xml file into jar or war, otherwise, these files will not be packaged. (maven thinks src/main/java is just the source code path of java). There are many ways on the network, I probably tried, several ways can be, you can choose one.

 

Method 1, where **/* is written to ensure that resource files in subdirectories at all levels are packaged.

Xml code

<build>  
    <finalName>test</finalName>  
    <!--  
    This way you can put all of them together. xml Files, packaged to the appropriate location.  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
                <include>**/*.tld</include>  
            </includes>  
            <filtering>false</filtering>  
        </resource>  
        <resource>  
            <directory>src/main/java</directory>  
            <includes>  
                <include>**/*.properties</include>  
                <include>**/*.xml</include>  
                <include>**/*.tld</include>  
            </includes>  
            <filtering>false</filtering>  
        </resource>  
    </resources>  
</build> 

 

Method 2, using build-helper-maven-plugin plug-in

Xml code

<build>  
    ...  
    </plugins>  
        ...  
        <!--  
        this plugin Can be used  
        Take advantage of this plugin,Put the source code in xml Documents,  
        Packing to the appropriate location, mainly for packaging Mybatis Of mapper.xml file   
        -->  
        <plugin>  
            <groupId>org.codehaus.mojo</groupId>  
            <artifactId>build-helper-maven-plugin</artifactId>  
            <version>1.8</version>  
            <executions>  
                <execution>  
                    <id>add-resource</id>  
                    <phase>generate-resources</phase>  
                    <goals>  
                        <goal>add-resource</goal>  
                    </goals>  
                    <configuration>  
                        <resources>  
                            <resource>  
                                <directory>src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>  

 

Method 3, maven-resources-plugin plug-in

 

Xml code

<build>  
    ...  
    </plugins>  
        ...  
        <!--  
        this plugin Can be used  
        Take advantage of this plugin,Put the source code in xml Files, packaged to the appropriate location,  
        This is mainly for packing. Mybatis Of mapper.xml file   
        -->  
        <plugin>  
            <artifactId>maven-resources-plugin</artifactId>  
            <version>2.5</version>  
            <executions>  
                <execution>  
                    <id>copy-xmls</id>  
                    <phase>process-sources</phase>  
                    <goals>  
                        <goal>copy-resources</goal>  
                    </goals>  
                    <configuration>  
                        <outputDirectory>${basedir}/target/classes</outputDirectory>  
                        <resources>  
                            <resource>  
                                <directory>${basedir}/src/main/java</directory>  
                                <includes>  
                                    <include>**/*.xml</include>  
                                </includes>  
                            </resource>  
                        </resources>  
                    </configuration>  
                </execution>  
            </executions>  
        </plugin>     
        ...  
    </plugins>       
    ...  
</build>  

 

For example, using IDEA front desk to display views using JSP pages requires the following configuration, otherwise 404 will be prompted, and no pages will be found.

POM file:

<!-- Introduce springboot embedded Tomcat Yes JSP Analysis Pack -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!-- servlet Dependent jar package start -->
 <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- servlet Dependent jar package end -->

<!-- jsp Dependent jar package start -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
</dependency>
<!-- jsp Dependent jar package end -->

<!-- jstl Label-dependent jar package start -->
<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>
</dependency>
<!-- jstl Label-dependent jar package end -->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <!-- Use IDEA Development JSP,To prevent pages from being missed,404 error occurred,The following configuration must be added -->
        <!--
        maven Packing configuration resources:
        -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <!-- This means that src/main/webapp All the following files(**/*.*)All compiled to META-INF/resources Directory - >
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
</build>

application.properties:

#The front view uses jsp to configure the jsp prefix and suffix
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

Controller:

@Controller
public class JspController {

    @RequestMapping("/boot/index")
    public String index(Model model){
        model.addAttribute("msg","spring boot Integrate JSP");
        return "index";
    }
}

JSP page:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>INDEX</title>
</head>
<body>
<div>
    ${msg}
</div>
</body>
</html>

Effect:

Posted by mlewis on Mon, 04 Feb 2019 11:06:16 -0800