Intellij idea 解决 maven工程的部分依赖问题

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>

注意事项

  1. 如果web-service的class变更后,在不全量执行的情况下,需要对web-service进行maven package操作
  2. Intellij Idea 导入工程后,需要手动添加对外部lib包下面的jar的依赖。

    工程代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值