Intellij idea 解决 maven工程的部分依赖问题
如果项目构建中遇到 web-app依赖web-service中的部分class打包的API jar。如下图描述:
通过Maven插件完成部分class的打包操作:
Maven-jar-plugin实现部分class打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>api</classifier>
<includes>
<include>**/service/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
引发的问题
Intellij Idea 无法通过Project Settings实现依赖部分class生产的jar。
- 通过maven dependency 进行依赖,在多module的工程中,会导致Intellij Idea会把整个 service工程依赖进来。
<dependency>
<groupId>com.study</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>api</classifier>
</dependency>
解决办法
由于web-app只是部分依赖web-service。按照intellij idea的严格意义上讲,其实就是不依赖。
- 删除web-app对web-service的依赖。
- 通过 maven-antrun-plugin 进行打包后拷贝。
- web-app增加对外部jar的编译依赖。
web-service打包拷贝
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-lib-src-webapps</id>
<phase>package</phase>
<configuration>
<tasks>
<copy file="${basedir}/target/${project.artifactId}-${project.version}-api.jar" todir="${basedir}/../web-app/src/main/webapp/WEB-INF/lib">
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
web-app编译依赖
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>${basedir}/src/main/webapp/WEB-INF/lib</extdirs>
</compilerArguments>
</configuration>
</plugin>
注意事项
- 如果web-service的class变更后,在不全量执行的情况下,需要对web-service进行maven package操作
Intellij Idea 导入工程后,需要手动添加对外部lib包下面的jar的依赖。