Jan-Olav Eide
Jan-Olav Eide

Reputation: 403

Creating a subset of an artifact

I have a maven project from which I require to produce a jarfile compiled with Java 7 for all the classes, and another jarfile for a subset of the classes compiled with Java 6. I know that I should produce ony one artifact pr project, what is the best was to split this up to achive my goal ?

Upvotes: 1

Views: 131

Answers (1)

Pavel
Pavel

Reputation: 3539

Does it matter to you if you have to run two separate builds to produce both artifacts? If not, you could easily achieve this with Maven profiles. You could set up each JDK profile to produce artifacts with different classifiers to distinguish them.

Personally, I prefer not to do use profiles for this. Two alternative approaches would be to have either a single Maven module with plugin customisations to perform two separate sets of compilation and artifact packaging operations, or to move to a multi-module Maven build with explicit separation based on the target JDK. (See this answer for example.) The former would make the pom.xml harder to maintain but result in a single neater structure. You might find that you spend a lot of time tweaking includes/excludes to specify what goes into each artifact. The latter would result in a more complicated project structure but there would be no doubts what source files go into what artifact.

The idiomatically correct Maven approach would be to split your codebase into a multi-module project with separate Java 6 and 7 modules, each with its own pom.xml. Each can specify different source/target version and only contain the appropriate source files. The Java 7 module can depend on and re-package the Java 6 classes into its own artifact (using dependency/assembly plugins), or even just reference the Java 6 module's sources directly using a relative path.

Upvotes: 1

Related Questions