J2EE模块中的jsp资源的maven打包和引用

这里接上一篇,介绍如何将一个j2ee的Maven模块中的资源,包括jsp,打包到一个jar包中,提供给其他项目引用。

1、被依赖的项目

1.1 代码修改

被依赖的项目,采用上一篇的module,新建几个测试的jsp,如下。
child/childPub.jsp

  
  
  
      
    Hello World!  
  
<%  
    String tmpChildPubStr = "Hello World!";  
%>  
  
    

child/childPub.jsp: <%=tmpChildPubStr%>

indexPub.jsp

  
  
  
      
    Hello World!  
  
<%  
    String tmpPubStr = "Hello World!";  
%>  
  
    

indexPub.jsp: <%=tmpPubStr%>

修改pom.xml文件,如下:

  
  
    4.0.0  
  
    com.lfqy.j2ee  
    mvn-tmct  
    1.0-SNAPSHOT  
  
                        commons-lang  
            commons-lang  
            2.6  
                                        src/main/webapp  
                            WEB-INF/**  
                        META-INF/resources  
            false  
                  

从这个配置,不难看出,实际的含义是将src/main/webapp目录下的资源打包到META-INF/resources目录下,但是不包含WEB-INF子目录下的内容。

1.2 Maven编译和install

到这里,点击IDEA编辑器的右侧Maven侧边栏,选中当前的module,先执行clean,再执行compile之后,可以看出src/main/webapp目录下的资源已经按照我们预期设定的,打包到了target目录下。如下图。

image.png

然后,在当前module的根目录(pom.xml文件所在的目录)下,执行mvn install命令,如下。

$ mvn install
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.lfqy.j2ee:mvn-tmct >-----------------------
[INFO] Building mvn-tmct 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mvn-tmct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 6 resources to META-INF/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mvn-tmct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mvn-tmct ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/chengxia/Developer/TrialProject/mvn-tmct/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mvn-tmct ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-tmct ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn-tmct ---
[INFO] Building jar: /Users/chengxia/Developer/TrialProject/mvn-tmct/target/mvn-tmct-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mvn-tmct ---
[INFO] Installing /Users/chengxia/Developer/TrialProject/mvn-tmct/target/mvn-tmct-1.0-SNAPSHOT.jar to /Users/chengxia/Developer/Java/tools/apache-maven-3.6.0/repository/com/lfqy/j2ee/mvn-tmct/1.0-SNAPSHOT/mvn-tmct-1.0-SNAPSHOT.jar
[INFO] Installing /Users/chengxia/Developer/TrialProject/mvn-tmct/pom.xml to /Users/chengxia/Developer/Java/tools/apache-maven-3.6.0/repository/com/lfqy/j2ee/mvn-tmct/1.0-SNAPSHOT/mvn-tmct-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.221 s
[INFO] Finished at: 2022-11-27T01:18:21+08:00
[INFO] ------------------------------------------------------------------------
$ 

2、依赖的项目

2.1 代码修改

参照前面的步骤创建一个基于Maven的J2EE项目mvn-tmct-parent
pom.xml

  
  
    4.0.0  
  
    com.lfqy.j2ee  
    mvn-tmct-parent  
    1.0-SNAPSHOT  
  
                        com.lfqy.j2ee  
            mvn-tmct  
            1.0-SNAPSHOT  
            

新建一个main.jsp,用于测试项目是否正常。

  
  
  
      
    Hello World!  
  
<%  
    String tmpStr = "Hello World!";  
%>  
  

main.jsp: <%=tmpStr%>

新建一个mainP.jsp,用于测试是否可以引用Maven依赖jar包resource根目录下的jsp是否正常。

  
  
  
      
    Hello World!  
  
<%  
    String tmpMainPStr = "Hello World!";  
%>  
  

mainP.jsp: <%=tmpMainPStr%>

child page:

<%@include file="indexPub.jsp" %>

新建一个mainPP.jsp,用于测试是否可以引用Maven依赖jar包resource目录子目录下的jsp是否正常。

  
  
  
      
    Hello World!  
  
<%  
    String tmpMainPPStr = "Hello World!";  
%>  
  

mainPP.jsp: <%=tmpMainPPStr%>

child page:

<%@include file="child/childPub.jsp" %>

2.2 编译和启动

在当前module的根目录下执行如下命令,用于拷贝依赖到lib目录下。

mvn-tmct-parent chengxia$ mvn dependency:copy-dependencies -DoutputDirectory=/Users/chengxia/Developer/TrialProject/mvn-tmct-parent/src/main/webapp/WEB-INF/lib -DincludeScope=runtime
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< com.lfqy.j2ee:mvn-tmct-parent >--------------------
[INFO] Building mvn-tmct-parent 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ mvn-tmct-parent ---
[INFO] Copying commons-lang-2.6.jar to /Users/chengxia/Developer/TrialProject/mvn-tmct-parent/src/main/webapp/WEB-INF/lib/commons-lang-2.6.jar
[INFO] Copying mvn-tmct-1.0-SNAPSHOT.jar to /Users/chengxia/Developer/TrialProject/mvn-tmct-parent/src/main/webapp/WEB-INF/lib/mvn-tmct-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.765 s
[INFO] Finished at: 2022-11-27T01:40:14+08:00
[INFO] ------------------------------------------------------------------------
mvn-tmct-parent chengxia$ 

到这里,目录结构如下。


image.png

项目启动后,浏览器中访问http://localhost:8080/main.jsp,如下。

image.png

访问http://localhost:8080/mainP.jsp,如下。

image.png

访问http://localhost:8080/mainPP.jsp,如下。

image.png

到这里,可以看出,maven依赖中的jsp可以被正常引用。
实际上,也可以直接访问maven依赖中的jsp。浏览器中访问http://localhost:8080/indexJava.jsp,如下。

image.png

你可能感兴趣的:(J2EE模块中的jsp资源的maven打包和引用)