Maven / Maven Interview questions II
A Build profile represents a set of configuration that is used to set or override default values of a Maven build. Using a build profile, a maven build can be customized for various application environments such as Production, Testing, Staging and development.
There are 4 different types of build profile.
- Per Project. Defined in the project's POM itself (pom.xml).
- Per User. Defined in the Maven-settings xml file (%USER_HOME%/.m2/settings.xml).
- Global. Defined in the global Maven-settings (${maven.home}/conf/settings.xml).
- Profile descriptor. a descriptor located in project basedir (profiles.xml).
A profile can be triggered or activated in several ways.
- Explicitly.
- Through Maven settings.
- Based on environment variables.
- OS settings.
- Present or missing files.
There are 4 different types of build profile.
- Per Project. Defined in the project's POM itself (pom.xml).
- Per User. Defined in the Maven-settings xml file (%USER_HOME%/.m2/settings.xml).
- Global. Defined in the global Maven-settings (${maven.home}/conf/settings.xml).
- Profile descriptor. a descriptor located in project basedir (profiles.xml).
There are 3 built-in build lifecycles.
- The default lifecycle handles your project deployment.
- the clean lifecycle handles project cleaning.
- the site lifecycle handles the creation of the project's site documentation.
A Build Lifecycle is Made Up of one or more phases.
package phase pulls the compiled code and package it to a distributable format, such as a JAR.
The below is the command to package a maven project.
mvn package
The following are the phases of site lifecycle in Maven.
- pre-site,
- site,
- post-site,
- and site-deploy.
Maven local repository is located in your local system and is created by the maven when you run any maven command.
By default, maven local repository is %USER_HOME%/.m2 directory.
We can change the location of maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf.
Maven central repository is located on the web created by the apache maven community.
The path of central repository is: http://repo1.maven.org/maven2/.
The central repository serves a lot of common libraries that can be browsed using this url http://search.maven.org/#browse.
Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://. These repositories are set up by a third party to provide their artifacts for downloading.
A "remote" repository may also be an internal repository set up on a file or HTTP server within an organization, used to share private artifacts between development teams and for releases.
Using jar-with-dependencies as the descriptorRef of your assembly-plugin configuration, we can create a JAR along its dependencies.
This built-in descriptor creates an assembly with the classifier jar-with-dependencies using the JAR archive format.
Below is the subset of pom.xml that includes assembly-plugin configuration along with jar-with-dependencies descriptor.
<build> <plugins> <!-- other plugin configuration --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
Ant and Maven are build tools provided by Apache Foundation. The purpose of these tools is to simplify the build process of a project.
Apache Ant | Maven |
Apache Ant is a toolbox. | Maven is a framework. |
Ant does not provide a formal conventions for project directory structure. | Maven has formal conventions. |
Ant does not have lifecycle; you have to add sequence of tasks manually. | Maven build has lifecycle. |
Ant is procedural and requires explicit information on what to do and when to do through code and also the order of the steps. | Maven is declarative and the process can be configured in the pom.xml file. |
Ant scripts are not reusable. | Maven plugins are reusable. |
Ant is mainly a build tool. | Maven is a project management tool. |
To build an executable jar using maven, we need to add manifest configuration to the maven-assembly-plugin.
The below subset of pom.xml configure maven-assembly-plugin to create a single executable jar.
<build> <plugins> <!-- Build an executable JAR --> <plugin> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>fully-qualified-MainClass</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
By default, Maven configures and lookup into the src/main/resources directory for your project resources.
Also additional resources directories could be specified by adding the configuration to the project's Pom.xml.
<project> ... <build> ... <resources> <resource> <directory> src/proj_resources </directory> </resource> </resources> ... </build> ... </project>
We may configure several resources directories by adding multiple resource elements.
The HTTP proxy settings need to be specified in the file settings.xml. The encrypted passord could be specified in the file. Also there is a need to add the dependency jar wagon-http-lightweight-2.2.jar that retrieves the artifacts through http using Apache httpclient -4.x. The jar has to be added to the maven folder lib/ext.
Repository is a collection of artifacts (eg: jars). You can think of it as a mere storage / cache of various artifacts.
Dependency is a situation where your project dependent on another artifact to perform its task. (For example., compile, run, unit test etc.)
Maven POM contains the some of the following configuration.
- project version.
- project dependencies.
- plugins.
- goals.
- build profiles.
- developers and contributers.
- mailing list.
Archetype is a Maven plugin that creates a project structure as per its template.
The below is the command to create a new maven project based on an archetype.
mvn archetype:generate
Maven plugin helps in,
- Creating jar and war.
- Compiling source code.
- unit testing of code.
- Creating project documentation and reports.
There are 2 types of maven plugins.
-
Build plugins execute during the build and should be configured in the
element of pom.xml. - Reporting plugins execute during the site generation and it should be configured in the <reporting/> element of the pom.xml.
The Surefire Plugin is applied during the test phase of the build lifecycle to execute the unit tests of an application. It also generates reports.
By default, the reports are generated at ${basedir}/target/surefire-reports.
- Plain text file (*.txt)
- XML file (*.xml)
A maven goal represents a specific task that contributes to the building and managing of a Maven project. It may be bound to zero or more build phases.
A goal that does not bound to any build phase could be executed outside of the build lifecycle by direct invocation.
surefire:test, the only goal of the surefire plugin, runs the unit tests of an application.
By calling,
mvn test
In terms of version, when maven has downloaded a dependency project version 1.0, it will not download again the latest version of 1.0 while building the project. To facilitate the latest version the dependent project need to do upgrades it's version to 1.1.
In case of SNAPSHOT, Maven will automatically download the latest SNAPSHOT (:1.0-SNAPSHOT) of the dependency project everytime the project is built.
The clean lifecycle consists of the following phases.
- pre-clean,
- clean,
- and post-clean.
mvn:install copies your packaged Maven module to your local repository (by default, in ~/.m2/repository), to be accessed by other local Maven builds.
mvn:deploy uploads your packaged Maven module to another (usually remote) repository, to be accessed by others.
package: takes the compiled code and package it to its distributable format, such as a JAR.
install: installs the package into the local repository, for use as a dependency in other projects locally.
All Maven POMs inherit defaults from the Super POM.
For a basic Java project, we just need the Simplest POM, that defines a groupId, artifactId, and version, the three required coordinates for every project. you don't have to customize else as the other configurations will be inherited form the Super POM.
The Effective POM refers the merge between the Super POM and the POM from The Simplest POM.
In Maven2, exclude a every single transitive dependency using separate <exclusion> list which is complex.
In Maven3 the use of wildcard is allowed on both groupId and artifactId that exclude all dependencies that propagates through to the module using this dependency.
<dependency> <groupId>${project.groupId}</groupId> <artifactId>TestApp</artifactId> <version>${project.version}</version> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
use the finalName tag to define/change the name of the war file in the web module that produces the war. See the below example.
<build> <finalName>MyWebApp</finalName> . . . </build>
Maven handles following aspects,
- Build.
- Documentation.
- Reporting.
- Dependencies.
- SCMs.
- Releases.
- Distribution.
- Mailing list.
To view/display execution debug output call Maven with X parameter or e parameter.
Maven is a build tool and also provides dependency management, standard project layout and project management.
Jenkins/Teamcity/Hudson is a continuous integration tool which is much more than build tool. You can setup your CI environment using CI tools and automatically build, test and deploy your Java project.
Setting inherited tag to false prevents the propagation to child POMs.
Run "mvn -X" command.
This command erase the target directory with all the build data before starting the building process.
A new project can be created using the command mvn archetype:generate .
Run the mvn compile command from the folder location where Pom.xml exists.
Maven does not support multiple inheritance and a project can have only one parent. The parent project can have its own hierarchy.
Pom packaging is used aggregate other projects and it acts as a container for sub modules within that project.
Pom packaging is also used to identify the parent in non-modular context.
Maven relocation identifies the projects/artifacts that are relocated or moved on the repository.
<project> <modelVersion>4.0.0</modelVersion> <groupId>bar</groupId> <artifactId>foo</artifactId> <version>1.0</version> <distributionManagement> <relocation> <groupId>org.bar</groupId> </relocation> </distributionManagement> </project>
Yes. The local repo folder name can be changed by updating in the settings.xml. localRepository property in the file holds the path of the build system's local repository. The default value is ${user.home}/.m2/repository and it can be changed.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>${user.home}/.m2/repository</localRepository> </settings>
mvn package
takes the compiled code and package it in its distributable format, such as a JAR.
mvn install
installs the package into the local repository, for use as a dependency in other projects locally.
JAXB2 maven plugin wraps and enhances the JAXB Schema Compiler (XJC) and allows compiling XML Schemas (as well as WSDL, DTDs, RELAX NG) into Java classes in Maven builds.
This is plugin is used to generate web service clients from WSDL by generating domain Java classes. Plugin can marshall (Java to xml ) as well as unmarshal (xml to Java).
BOM (Bill Of Materials) is a special kind of POM used to control the versions of a project's dependencies and provide a central place to define and update those versions.
BOM provides the flexibility to add a dependency to our module without worrying about the version that we should depend on.