手把手教你一个maven的war项目如何引用另外一个war项目

我们都知道一个war项目可以部署到服务器,pom文件里面可以引用许多依赖jar包。但是默认的都是jar类型的,如果一个大的web项目,分模块开发,我想一个war项目引用一个另外一个war项目的jap,css和.class文件,这个可以么???答案是肯定的,不过需要一些特殊的配置。需求知道了,好,直接上代码!!!

1,首先创建两个war类型的maven项目。一个普通的jar类型的maven项目(忽略报错,其实里面啥都没错,就是显示x,不用管)

手把手教你一个maven的war项目如何引用另外一个war项目_第1张图片

2,

需要在子war项目的pom和父war项目的pom文件中配置一下

这里科普一下,因为jar类型会把class文件依赖进去,而不会把jsp这些文件依赖进去,war类型的反之,所以,需要配置一下,把war文件配置成既是war又是jar的maven项目。父项目不用管,只管需要引入的子war项目,需要在子war项目里面再写一个pom-war.xml文件,大致和pom.xml一样,只需要稍微改动,可以复制pom然后修改即可。

fu 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.0modelVersion>
  <groupId>com.lzhgroupId>
  <artifactId>fuartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>

  <dependencies>
      
    <dependency>    
        <groupId>javax.servletgroupId>    
        <artifactId>servlet-apiartifactId>    
        <version>2.5version>    
        <scope>providedscope>    
    dependency>

    <dependency>    
        <groupId>com.lzhgroupId>    
        <artifactId>ziartifactId>    
        <version>0.0.1-SNAPSHOTversion>    
    dependency>

     <dependency>    
        <groupId>com.lzhgroupId>    
        <artifactId>worldartifactId>  
        <type>wartype>
        <version>0.0.1-SNAPSHOTversion>    
     dependency>
     <dependency>    
        <groupId>com.lzhgroupId>    
        <artifactId>worldartifactId>  
        <type>jartype>
        <version>0.0.1-SNAPSHOTversion>    
     dependency>
  dependencies>
  <build>
    <plugins>
        <plugin>
            <artifactId>maven-war-pluginartifactId>
            <configuration>
                <version>3.0version>
            configuration>
        plugin>
    plugins>
    <defaultGoal>compiledefaultGoal>
  build>
project>

world pom-war.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.0modelVersion>
    <groupId>com.lzhgroupId>
    <artifactId>worldartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>warpackaging>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.7java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.1.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-webartifactId>
            <version>4.1.3.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-configartifactId>
            <version>4.1.3.RELEASEversion>
        dependency>
    dependencies>

    <build>

        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <excludes>
                    <exclude>**/*.javaexclude>
                    <exclude>**/.svn/*exclude>
                excludes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>

        resources>
        <plugins>
            <plugin>
                <artifactId>maven-war-pluginartifactId>
                <configuration>
                    <version>3.0version>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.6source>
                    <target>1.6target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <configuration>
                    <failOnMissingWebXml>falsefailOnMissingWebXml>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <configuration>
                    
                    <archiveClasses>falsearchiveClasses>
                    
                    <webResources>
                        <resource>
                            <directory>src/main/resourcesdirectory>
                            <targetPath>WEB-INF/classestargetPath>
                            <filtering>truefiltering>
                        resource>
                    webResources>
                configuration>
            plugin>

        plugins>
    build>
project>

world 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.0modelVersion>
    <groupId>com.lzhgroupId>
    <artifactId>worldartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.7java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
            <version>4.1.1version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-webartifactId>
            <version>4.1.3.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.securitygroupId>
            <artifactId>spring-security-configartifactId>
            <version>4.1.3.RELEASEversion>
        dependency>
    dependencies>

    <build>

        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <excludes>
                    <exclude>**/*.javaexclude>
                    <exclude>**/.svn/*exclude>
                excludes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>

        resources>
        <plugins>
            <plugin>
                <artifactId>maven-war-pluginartifactId>
                <configuration>
                    <version>3.0version>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>1.6source>
                    <target>1.6target>
                    <encoding>UTF-8encoding>
                configuration>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <executions>
                    <execution>
                        <id>make-a-jarid>
                        <phase>packagephase>
                        <goals>
                            <goal>jargoal>
                        goals>
                    execution>
                executions>
            plugin>


            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <executions>
                    <execution>
                        <id>attach-sourcesid>
                        <goals>
                            <goal>jar-no-forkgoal>
                        goals>
                    execution>
                executions>
            plugin>


        plugins>
    build>
project>

zi 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.0modelVersion>
  <groupId>com.lzhgroupId>
  <artifactId>ziartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <build/>
  <dependencies>
      
    <dependency>    
        <groupId>javax.servletgroupId>    
        <artifactId>servlet-apiartifactId>    
        <version>2.5version>    
        <scope>providedscope>    
    dependency>
  dependencies>
project>

注意前提是引入的依赖需要安装到本地仓库。
然后再world项目中新建jsp文件
手把手教你一个maven的war项目如何引用另外一个war项目_第2张图片

最后,更新maven项目,把fu项目部署到tomcat。
你会发现,world的jsp文件都进来了
手把手教你一个maven的war项目如何引用另外一个war项目_第3张图片

手把手教你一个maven的war项目如何引用另外一个war项目_第4张图片

注意,这里主要运用了maven的插件。下面把插件的功能说明一下: 主要看注释部分

附一个比较详细的博客:http://blog.csdn.net/liutao363071094/article/details/49021525
http://blog.csdn.net/iflow/article/details/41693057

<plugin>  
                <groupId>org.apache.maven.pluginsgroupId>  
                <artifactId>maven-war-pluginartifactId>  
                <configuration>  
                      
                    <archiveClasses>truearchiveClasses>  
                    <webResources>  
                          
                        <resource>  
                              
                            <directory>src/main/configdirectory>  
                            <targetPath>WEB-INF/classestargetPath>  
                            <filtering>truefiltering>  
                        resource>  
                    webResources>  
                    <archive>  
                        <manifest>  
                            <addDefaultImplementationEntries>trueaddDefaultImplementationEntries>  
                            <addDefaultSpecificationEntries>trueaddDefaultSpecificationEntries>  
                        manifest>  
                        <manifestEntries>  
                            <Built-On>${timestamp}Built-On>  
                        manifestEntries>  
                    archive>  
                configuration>  
                <executions>  
                    <execution>  
                        <id>generate-manifestid>  
                        <phase>prepare-packagephase>  
                        <goals>  
                            <goal>manifestgoal>  
                        goals>  
                    execution>  
                executions>  
            plugin>  

你可能感兴趣的:(maven)