Springboot或Java web项目使用maven打包时引入外部源

场景或需求:

    现有项目A(Springboot项目,项目名称project1-web)和项目B(JavaWeb项目,项目名称project2-web),现在使用maven 对项目A进行编译和打包,项目A中没有页面资源文件(html,jsp,图片,css等),而项目B则有页面资源文件。

项目A目录结构示意图:

---project1-web

----target

------project1-web.jar

----pom.xml

项目B目录结构示意图:

--project2-web

----WebContent

------js

--------jquery.js

------a.jsp

----WEB-INF

------web.xml

----pom.xml

    现在希望在项目A中引入项目B页面资源文件,以达到共享的目的,避免项目在更新时要修改多个地方,导致操作繁琐和及可能造成代码不一致。如何操作?

解决:

解决该问题或需求的方法是在maven配置文件pom.xml中,对资源进行控制。大体有3种办法,分别是

1、<build>节点的<resources>;

2、使用maven 插件maven-resources-plugin,拷贝resources;

3、使用maven插件build-helper-maven-plugin,增加resources。与2类似,只是使用的插件不一样罢了。

示例:

1、使用<build>节点的<resources>

<resource>
    <directory>../project2-web/WebContent</directory>
    <targetPath>/META-INF/resources</targetPath>
    <includes>
        <include>**/**</include>
    </includes>
</resource>    
错误的写法:

<resource>
	<directory>../project2-web/WebContent</directory>
<!--  				<excludes>
		<exclude>META-INF/resources/WEB-INF/*.xml</exclude>
		<exclude>META-INF/resources/WEB-INF/conf/**</exclude>
	</excludes>  -->
	 <targetPath>${project.build.directory}/META-INF/resources</targetPath>
	<includes>
		<include>**/**</include>
	</includes>		
<!-- 				<filtering>false</filtering> -->		
</resource>		
    目标路径应为相对路径,即在输出jar包中的路径,而不是磁盘路径。如果按上面的写法,则会输出到构建输出目录(target)的META-INF/resources目录中,而不会输出到jar包中。
2、使用maven 插maven-resources-plugin

<plugin>  
	<artifactId>maven-resources-plugin</artifactId>  
	<version>2.5</version>  
	<executions>  
		<execution>  
			<id>copy-webcontent</id>  
			<phase>process-sources</phase>  
			<goals>  
				<goal>copy-resources</goal>  
			</goals>  
			<configuration>  
				 <!--拷贝文件到该位置—->
				<outputDirectory>${project.build.directory}</outputDirectory>  
				<resources>  
					<resource>  
						<directory>../project2-web/WebContent</directory>  
						<includes>  
							<include>**/**</include>  
						</includes>  
						 <!—输出到jar包中的位置(在jar包中的路径)-->
						<targetPath>/META-INF/resources</targetPath>
					</resource>  
				</resources>  
			</configuration>  
		</execution>  
	</executions>  
</plugin>     
错误的写法:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>  
	<artifactId>maven-resources-plugin</artifactId>  
	<executions>
		<execution>
			<id>copy-webcontent</id>
			<phase>process-source</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/META-INF/resources</outputDirectory>
				<resources>
					<resource>
						<directory>../project2-web/WebContent</directory>
						<includes>
							<include>**/**</include>
						</includes>
						  <!--缺少输入路径指定 -->
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>
     这里错误的原因是缺少指定的输入路径(元素:targetPath),导致不会输入到jar包中。

   注意:outputDirectory不是指输出到jar包中的路径,而是拷贝过来的文件存放的路径,位置不限。但指定输出到jar包中的路径不可缺少

3、使用maven插件build-helper-maven-plugin

<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>../project2-web/WebContent</directory>  
						<includes>  
							<include>**/**</include>  
						</includes>  
						<targetPath>/META-INF/resources</targetPath>
					</resource>  
				</resources>  
			</configuration>  
		</execution>  
	</executions>  
</plugin>    
     同样注意,targetPath不可缺少。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值