Ant for Maven - several build goals

I have an Ant build that is currently being converted to Maven. However, Ant build has 2 build goals - one that builds the entire application, and one that builds the JAR from some of these files (just a few). In Ant, it’s easy to cope with several build goals, but I’m trying to determine the best way to handle this in Maven.

I could split a subset of the files into a second project, and it would have its own POM. Then the first project may depend on this. However, since the subset of the files is so small (less than 10), it looks like it might be redundant to have a completely new project for it.

Are there any other ways to handle this?

+50
maven build ant target
Jan 25
source share
3 answers

Your first thought is correct. Divide 2 parts into 2 projects.

The philosophy of maven is that each project should build one and only artifact (jar, war, whatever)

Perhaps you could hack something together, so that you only have one maven project creating 2 atrifacts, but that will be a hack.

You can call ant from maven, so if you really want to, I suggest you start looking at the maven ant plugin. The artifact identifier is "maven-antrun-plugin"

+22
Jan 25
source share

You can do this with profiles ...

If you really wanted to use two separate profiles and configure the JAR plugin to include and exclude class and package name patterns, you can easily do this by putting something like this in your POM:

<profiles> <profile> <id>everything</id> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>everything</classifier> <includes> <include>**/*</include> </includes> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>only-library</id> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>only-library</classifier> <excludes> <exclude>**/Main*</exclude> </excludes> </configuration> </plugin> </plugins> </build> </profile> </profiles> 

Also: if this sounds like a great configuration, the polyglot Maven support for Groovy POMs is ready. It will significantly reduce the number of lines.

You would put this at the end of your pom.xml (with the project element) and add two profiles. The first "all" profile really exists to demonstrate the configuration. This "all" profile is superfluous because it simply duplicates the default JAR plugin execution behavior of the JAR. The second "only-library" profile excludes any class in any package that starts with the text "Main". To call these profiles:

 mvn package -Peverything mvn package -Ponly-library 

I tested this with an example application that comes with Maven Chapter 6 for example , and when you run any of these commands, a JAR file will be created in $ {basedir} / target, which has a classifier. Since the purpose of the JAR plugin is related to the phase of the package in the default maven life cycle, these two profiles are going to change the configuration for this plugin.

Or you can do it with two JAR plugins ...

If you need to create two JARs without using profiles. You can link the target of the JAR banner to the package life cycle phase several times and use a different configuration for each configured execution. If you configure two separate executions, each execution has a configuration block for a particular execution, so you can provide a unique identifier and include / exclude a template for each execution.

Here is the assembly element that you would use to add both custom JARs to the life cycle phase of the life cycle. The implementation of this project with packaging "jar" will lead to the fact that the goal of the jar will be fulfilled three times. Once the default lifecycle binding, then twice for two custom, classified JARs.

  <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <id>only-library</id> <goals><goal>jar</goal></goals> <phase>package</phase> <configuration> <classifier>only-library</classifier> <excludes> <exclude>**/Main*</exclude> </excludes> </configuration> </execution> <execution> <id>everything</id> <goals><goal>jar</goal></goals> <phase>package</phase> <configuration> <classifier>everything</classifier> <includes> <include>**/*</include> </includes> </configuration> </execution> </executions> </plugin> </plugins> </build> 

If you are not talking about including a different set of classes in each artifact, you will want to use Maven Assemblies. If you want to know the details of assemblies, there is a chapter at the end of this answer mentioned in Maven: The Complete Reference. Honestly, I don't think this particular chapter is a good introductory link; in fact, I had many reports that this chapter is almost unreadable (and we are working on its removal). If you want to use assemblies, I would recommend the Maven Assembly Plugin documentation . In the left navigation menu you will see a list of sample assembly descriptors.

Disclaimer: (Please) do not do this. If you are creating two difference JARs with two different sets of classes, I highly recommend that you split the project into two interdependent modules.

While you can do this with profiles, it will be easier for you to split the project into two (actually three). In the long run, there will be problems that you run into as your applications scale. You will be responsible for defining this list of manual classes and packages that will be included in each of your classified JARs.

Minimum Overhead is a simple parent project that references two separate modules. If you look at Maven’s free book on examples, we’ll show you how to make the transition between a single-module and multi-module project. Chapters 3-5 focus on single-module projects and Chapter 6 shows you how to combine these components of one module into a larger multi-module project.

For more information:

You ask the following topics: here are some links that will contain more detailed information for each:

Maven JAR Plugin: http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html

Maven Multi-Module Projects: Chapter 6 of Maven's Case Study and Section 3.6.2 of Maven: A Complete Reference .

Maven Life Cycle (a jar is required for packaging if your package is a "jar"): Maven Section 3.5.2 on the Basics Example and Chapter 4 of Maven: full link

Maven Assemblies: first, the Maven Assembly Plugin site , then Chapter 8 of Maven: a complete reference for some heavy (almost too heavy) parts.

+66
Jan 26
source share

You have 2 options:

If a subset is just a set of resources, then I would not become a separate module.

If a project always depends on a subset that is packaged in a single way, then the subset is a good candidate to become a module .

If a subset is repackaged into several different “aromas”, I would define assemblies for each “aroma” and qualify the names of artifacts using a “classifier”, see the coordinates of maven .

Finally, you can use profiles to determine which assemblies are created; your default profile can only create the “flavor” of the original artifact that is required during development.
A “full” profile can create all “aromatic” artifact variants for final deployment after development is complete.

+5
Jan 25
source share



All Articles