maven build resource

In the jar package resources in resource use as UrlClassloader  occur when resource files in the src has not been maven into the package, so there is this article

 

1 https://www.cnblogs.com/pixy/p/4798089.html

 

Maven project build time, if there is no special configuration, Maven will find and deal with all types of documents in accordance with the standard directory structure .

 

src/main/java和src/test/java 

All * .java files in these directories will be compiled separately in comile and test-comiple stage , the results are compiled into a target / classes and targe / test-classes directory, but the two other files in the directory will It is ignored.

 

src/main/resouces和src/test/resources

The two files in the directory are also copied to the target / classes and target / test-classes directory .

 

target/classes

All plug-packaged content will default to this directory into the jar or bag war package.

 

 

Standard Maven project directory structure

  • src
    • main
      • java          source files 
      • resources     resource file
      • Resource filters filter file
      • config configuration file
      • scripts script file
      • webapp web application files
    • test
      • java     test source file
      • resources test resource file
      • filters filter test resource file
    • it integration testing
    • assembly    assembly descriptors
    • Service Site
  • target
    • generated-sources
    • classes
    • generated-test-sources
    • test-classes
    • xxx.jar
  • pom.xml
  • LICENSE.txt
  • NOTICE.txt
  • README.txt

 

Allocation of resources files

Resource file is a file in Java code to be used. In the implementation of the code will go to the specified location to find these files. As already said Maven default approach, but sometimes we need to customize the configuration.

Sometimes some configuration files are usually placed in src / main / java directory (such as mybatis table or hibernate mapping file) along with the .java file. Sometimes also want to put other resources in the directory copied to the classes directory. In these cases you need to modify the configuration file in the Pom.xml.

There are two ways:

  • First, add the <resources> In <build> element configuration.
  • Another is <build> of <plugins> child element disposed maven-resources-plugin processing such as resource files plug.

 

 

 

2 https://blog.csdn.net/u011781521/article/details/79052725

Under normal circumstances, we use resource files (various xml, properites, xsd files, etc.) are placed in src / main / resources below, take advantage of a package maven, maven can turn to the appropriate resource files are packaged in jar or war .


Sometimes, such as mybatis of mapper.xml file, we are used to it and Mapper.java put together, in src / main / java below, so take advantage of maven package, we need to modify the pom.xml file to the mapper.xml with files packaged into jar or war where, otherwise, these files will not be packaged. (Maven source path that src / main / java only the java)

The project structure is shown below, there are two UserMaper.xml files, different directory

 

 

 

By default, executed in POM.XML directory: mvn clean package packaging commands in the target \ classes directory will not UserMapper.xml package to the next mapper directory

 

The files are always under the resources directory packaged into a jar or bag war package

 

 

This time it will use mybatis some problems, xml file not found UserMapper corresponding solutions are summarized as follows:

(1) The resource configuration POM.XML xml also packed into the directory mapper

    <build>
       <! - Resource Directory ->    
        <resources>    
            <resource>    
                <! - Set the main resource directory ->    
                <directory>src/main/java</directory>    
 
                <-! Maven default life cycle stages of the implementation process-resources when certain resources maven-resources-plugin widget resource master file processing resources of the heads, only the type of processing resource contained follows ->     
				 <includes>
					  <include>**/*.xml</include>
				 </includes>  
                     
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->      
				<excludes>  
					<exclude>**/*.yaml</exclude>  
				</excludes>  
 
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,指定处理后的资源文件输出目录,默认是${build.outputDirectory}指定的目录-->      
                <!--<targetPath>${build.outputDirectory}</targetPath> -->      
   
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->    
                <filtering>true</filtering>     
            </resource>  			
        </resources> 	
  </build>

 

其中**/*这样的写法,是为了保证各级子目录下的资源文件被打包。resources是可以看做是容器,这个容器里面可以放很多个像resource这样的配置。而directory就是配置文件所在的路径,includes从英文单词角度看,就是包含的意思,此时在执行命令,就会把xml也打包到mapper目录下了
————————————————

(2)maven-resources-plugin插件

 

 

构建Maven项目的时候,如果没有进行特殊的配置,Maven会按照标准的目录结构查找和处理各种类型文件

 

src/main/java和src/test/java 

这两个目录中的所有*.java文件会分别在comile和test-comiple阶段被编译,编译结果分别放到了target/classes和targe/test-classes目录中,但是这两个目录中的其他文件都会被忽略掉。

 

src/main/resouces和src/test/resources

这两个目录中的文件也会分别被复制到target/classes和target/test-classes目录中

 

target/classes

打包插件默认会把这个目录中的所有内容打入到jar包或者war包中。

 

 

Maven项目的标准目录结构

  • src
    • main
      • java         源文件 
      • resources    资源文件
      • filters   资源过滤文件
      • config   配置文件
      • scripts   脚本文件
      • webapp   web应用文件
    • test
      • java    测试源文件
      • resources    测试资源文件
      • filters    测试资源过滤文件
    • it       集成测试
    • assembly    assembly descriptors
    • site    Site
  • target
    • generated-sources
    • classes
    • generated-test-sources
    • test-classes
    • xxx.jar
  • pom.xml
  • LICENSE.txt
  • NOTICE.txt
  • README.txt

 

资源文件的配置

资源文件是Java代码中要使用的文件。代码在执行的时候会到指定位置去查找这些文件。前面已经说了Maven默认的处理方式,但是有时候我们需要进行自定义的配置。

有时候有些配置文件通常与.java文件一起放在src/main/java目录(如mybatis或hibernate的表映射文件)。有的时候还希望把其他目录中的资源也复制到classes目录中。这些情况下就需要在Pom.xml文件中修改配置了。

可以有两种方法:

  • 一是在<build>元素下添加<resources>进行配置。
  • 另一种是在<build>的<plugins>子元素中配置maven-resources-plugin等处理资源文件的插件。

 

 

 

2 https://blog.csdn.net/u011781521/article/details/79052725

一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里。


有时候,比如mybatis的mapper.xml文件,我们习惯把它和Mapper.java放一起,都在src/main/java下面,这样利用maven打包时,就需要修改pom.xml文件,来把mapper.xml文件一起打包进jar或者war里了,否则,这些文件不会被打包的。(maven认为src/main/java只是java的源代码路径)

项目结构如下所示,有两个UserMaper.xml文件,所在目录不同

 

 

 

默认情况下,在POM.XML目录下执行: mvn clean package 打包命令在target\classes目录下不会把UserMapper.xml打包到下mapper目录下

 

而resources目录下的文件始终都会打包进jar包或war包

 

 

这个时候使用mybatis就会出一些问题,找不到UserMapper所对应的xml文件解决方法有如下几种:

(1)配置POM.XML的resource把xml也打包到mapper目录下

    <build>
       <!-- 资源目录 -->    
        <resources>    
            <resource>    
                <!-- 设定主资源目录  -->    
                <directory>src/main/java</directory>    
 
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->     
				 <includes>
					  <include>**/*.xml</include>
				 </includes>  
                     
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->      
				<excludes>  
					<exclude>**/*.yaml</exclude>  
				</excludes>  
 
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,指定处理后的资源文件输出目录,默认是${build.outputDirectory}指定的目录-->      
                <!--<targetPath>${build.outputDirectory}</targetPath> -->      
   
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->    
                <filtering>true</filtering>     
            </resource>  			
        </resources> 	
  </build>

 

其中**/*这样的写法,是为了保证各级子目录下的资源文件被打包。resources是可以看做是容器,这个容器里面可以放很多个像resource这样的配置。而directory就是配置文件所在的路径,includes从英文单词角度看,就是包含的意思,此时在执行命令,就会把xml也打包到mapper目录下了
————————————————

(2)maven-resources-plugin插件

 

 

Guess you like

Origin www.cnblogs.com/silyvin/p/12164432.html