Stuck
Stuck

Reputation: 12292

Maven 2 replace class implementation depending on profile

I have MailTransport.java and two classes extending it: LiveMailTransport.java and TestMailTransport.java. LiveMailTransport will really send emails while TestMailTransprot will only write them to the log for testing purpose.

Somewhere I do new MailTransport(); and I would like to replace every usage of MailTransport in my server-side code either with Live- or with TestMailTransport depending on the profile used for compiling (local, production, etc..).

(Similar to gwts "replace-with" on client side...)

How could I do that with Maven?

Upvotes: 4

Views: 1741

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328604

What you want is a factory which accepts a system property. If the system property isn't set, create an instance of LiveMailTransport. If the property is there, create an instance of TestMailTransport.

Proposed name of property: com.pany.app.enableTestMails

Boolean.getBoolean(String) is your friend.

Now configure the surefire plugin to set the property and you're done.

Upvotes: 3

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

If you're using Spring or some other dependency injection framework you could manipulate dependencies injected based on inclusion of corresponding configuration.

But if you want to do it with a plain bare bone Java application you could create multiple factories that will create corresponding instances of yoor MailTransport and place these factories into a different source folders. Then use build-helper-maven-plugin to add correspoinding source folder based on active profiles.

Upvotes: 0

khmarbaise
khmarbaise

Reputation: 97399

That sounds like a misuse of Maven, cause this looks more like dependency injection task (guice for example) but there is no relationship with Maven.

Upvotes: 0

Related Questions