Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

java - Intellij Maven multimodule project with classified dependency

I have a multi-module Maven project, with modules 'app' and 'domain'. App depends on Domain. The domain module has public and private code. Public code is in the package **/domain/api/**.

In the pom of the Domain module, I added an execution for the maven jar plugin, which generates an additional project artifact with classifier 'api' (containing only the public classes).

In the App module, I added a 'runtime' scoped dependency on the Domain module (so all classes are present at runtime) and I added a compile-time scoped dependency on the classified 'API'. This is to ensure the App modules only access public api code from the domain.

Everything compiles and runs. I have checked the contents of the api artifact from the domain module and it contains only the code intended to be public.

But in IntelliJ I'm still able to use (import) domain code from private packages. IntelliJ seems to ignore the classifier part. So IntelliJ compiles when I reference private code (which is wrong IMHO) and the Maven build fails (which is good).

Is this an IntelliJ issue or am I missing something?

The relevant pom part from de App module:

<dependency>
    <groupId>com.acme</groupId>
    <artifactId>sbp-domain</artifactId>
    <version>${project.version}</version>
    <classifier>api</classifier>
</dependency>
<dependency>
    <groupId>com.acme</groupId>
    <artifactId>sbp-domain</artifactId>
    <version>${project.version}</version>
    <scope>runtime</scope>
</dependency>
question from:https://stackoverflow.com/questions/65950563/intellij-maven-multimodule-project-with-classified-dependency

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Intellij has trouble with complex maven dependencies. Especially if you try to filter the original project.

I had the same problem with test-jar (the easy-way) as Intellij ignores the exclude. The IDEA-204719 tracks the progress.

khmrbase is right in the comments. You should create a separate project for your API. The only downside of that approach is that you have to duplicate (or further complicate your modules) for api-implementation shared test code. The solution for that could be the test-jar which as I explained doesn't work properly in Intellij.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...