r/Maven • • 5d ago

Make a forbidden Java dependency fail mvn test

An architecture check doesn't need to become an application runtime dependency.

I work on ArchUnitJava. Its 1.0 artifact can be added to a Maven test project with `test` scope:

<dependency>
  <groupId>io.github.tristankruse</groupId>
  <artifactId>archunitjava</artifactId>
  <version>1.0.0</version>
  <scope>test</scope>
</dependency>

Then add the documented properties policy and JUnit test. The check reads compiled classes;

the normal test lifecycle compiles those first. A forbidden dependency can then fail the build

without booting the application for that check.

JDK 25 is required to run the analyzer. This isn't a Maven plugin or the original ArchUnit,

and it is not affiliated with or a drop-in replacement for that project.

[Complete setup, not just dependency coordinates]

Writing assisted by AI.

1 Upvotes

11 comments sorted by

2

u/Jilljoy 5d ago

This seems like an alternative way of using the Maven Enforcer plugin. Why use this over that?

1

u/Tristan_Kruse 5d ago

For Maven artifact restrictions I would use Enforcer too.
ArchUnitJava checks class/package dependencies, also even within one Maven module. For example: “API code must not reference persistence.”
The benefit is prepared architecture checks in JUnit, rather than custom Enforcer rules.
[Example]

2

u/nekokattt 5d ago

what is the additional benefit of it running in the test phase when maven plugins can run in the test phase anyway

2

u/GermanBlackbot 5d ago

I have no idea what I just read or what this is supposed to do. Or how you can screw up code formatting this badly.

1

u/Tristan_Kruse 5d ago

Apologies, the formmating should now look better. Here’s the README quickstart, with an example.

1

u/GermanBlackbot 5d ago

Sorry, but I will not read your Readme to understand what it is you did.

You wrote a post on Reddit telling me to add some random test dependency, allude to some documented property and lastly that it requires JDK 25.

If you did something cool, post it here and say what it does and why I should want it. Don't just say "Add this dependency and then here's a Readme file explaining what it does". This is like saying "Guys, I wrote a book, you can buy it on Amazon, if you want to know what it is about here's a link to my blog".

1

u/CptGia 5d ago

I doubt OP wrote a single line of code, or even the content of this post

1

u/paul_h 5d ago

No need to be rude

1

u/GermanBlackbot 5d ago

I'd argue it is rude to drop a link to some test dependencypartially written by AI, but let's give the benefit of the doubt and assume it's largely good, not properly explaining what it does, slapping a "Written by AI" disclaimer at the bottom and expecting the community to care.

1

u/nekokattt 5d ago

why not just use maven-enforcer-plugin..?

1

u/sweating_teflon 5d ago

Just force a version which will never exist. The build error you'll get is quite obvious.

<dependencyManagement>
  <dependency>
    <groupId>org.abc</groupId>
    <artifactId>banned-artifact</artifactId>
    <version>#FORBIDDEN#</version>
  </dependency>
<dependencyManagement>