Sei sulla pagina 1di 12

What is the command to check the maven version?

mvn -version
Maven has a convention to place source code, compiled code etc.
Maven is mainly a project management tool , it has framework.
What is a MOJO?
A MOJO stands for Maven plain Old Java Object. Each MOJO is an executable goal
in Maven, and a plugin is a distribution of one or more related MOJOs.

) What is repository?
A repository is a directory or place where all the jars and pom.xml file are sto
red. There are 3 types of repository in Maven:
Local Repository
Central Repository
Remote Repository

) What is local repository?


Maven local repository is created by maven in your local system when you run any
maven comman.
What is POM?
POM stands for Project Object Model. The pom.xml file contains information of pr
oject and project configuration.

What is fully qualified artifact name of maven project?


<groupId>:<artifactId>:<version>

What is archetype?
Archetype is the maven plugin. It creates the project structure.

Mention the three build lifecycle of Maven?


Clean: Cleans up artifacts that are created by prior builds
Default (build): Used to create the application
Site: For the project generates site documentation

n Maven what are the two setting files called and what are their location?
In Maven, the setting files are called settings.xml, and the two setting files a
re located at
Maven installation directory: $M2_Home/conf/settings.xml
Users home directory: ${ user.home }/ .m2 / settings.xml
) List out the build, source and test source directory for POM in Maven?
Build = Target
Source = src/main/java
Test = src/main/test

5) Where do you find the class files when you compile a Maven project?
You will find the class files ${basedir}/target/classes/.

16) Explain what would the jar: jar goal do?


jar: jar will not recompile sources; it will simply just create a JAR from the t
arget/classes directory considering that everything else has been done
17) List out what are the Mavens order of inheritance?
The mavens order of inheritance is
Parent Pom
Project Pom
Settings
CLI parameters
18) For POM what are the minimum required elements?
The minimum required elements for POM are project root, modelVersion, groupID, a
rtifactID and version.

Q2. How can I change the default location of the generated jar when I command "
mvn package"?
Ans. By default, the location of the generated jar is in ${project.build.directo
ry} or in your target directory. We can change this by configuring the outputDir
ectory of maven-jar-plugin.

Q7. Where do we configure repositories in Maven ?


Ans. Within settings.xml in either MAVEN_HOME or .M2 directory.

Q9. How does Maven looks for a dependency or resource ?


Ans. It refers to the settings.xml to look for the repositories to look for the
resource. First It looks into the configured local repository, then it looks int
o the configured Remote repositories. If the resource is still not found , it lo
oks it within maven repository central i.e repo1.maven.org. If its still not fou
nd, it throws the exception saying "Unable to find resource in repository centra
l"

Q13. What is the difference between compile and install ?


Ans. Compile compiles the source code of the project
whereas
Install installs the package into the local repository, for use as a dependency
in other projects locally

Q14. How can we see Dependencies for the project and where exactly they are def
ined ?
Ans. Using
mvn dependency:tree

What is the default location of local repository ?


~/m2./repository
To generate maven project :mvn archetype:create -DarchetypeGroupId=org.apache.ma
ven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1
.0 -DgroupId=com.maventest -DartifactId=testnew -Dversion=1.0-SNAPSHOT - See mor
e at: http://www.searchcrone.com/2012/01/maven-interview-questions-and-answers#s
thash.nwdOzeBI.dpuf

How could we change the location of maven local repository ?


Ans :To change the default location of maven local repository,we have to edit se
tting.xml file.
Edit below section of setting.xml file :
<settings>
<! localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
- See more at: http://www.searchcrone.com/2012/01/maven-interview-questions-and-
answers/2#sthash.phm7hE4t.dpuf

What is maven remote repository ?


Ans :Maven remote repository, which is http://download.java.net/maven/2/.
When maven fails in local repository and central repository to find dependencies
files then it will go to maven remote repository.
We define remote repository in pom.xml file of project.
<repositories>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
How to define project dependencies in maven ?
Ans :We include all dependencies information in maven pom.xml file.We use below
pattern for this :
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
- See more at: http://www.searchcrone.com/2012/01/maven-interview-questions-and-
answers/2#sthash.phm7hE4t.dpuf
How to create Java Project using maven ?
Ans :To create Java project using maven follow below process :
1.Open Command line
2.Change directory where you would like to create your java project
3.Type blow command for creating java project :
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeA
rtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DgroupId=com.maventest
-DartifactId=searchcrone -Dversion=1.0-SNAPSHOT
Using above command a project directory will create name searchcrone.Above proje
ct will be web based project.
- See more at: http://www.searchcrone.com/2012/01/maven-interview-questions-and-
answers/3#sthash.eKiksYej.dpuf

The BUILD LIFE CYCLE ::


There are three built-in build lifecycles:
default,
clean
and
site.
The default lifecycle handles your project deployment,
the clean lifecycle handles project cleaning,
while the site lifecycle handles the creation of your project s site documentat
ion.
A Build Lifecycle is Made Up of Phases
Each of these build lifecycles is defined by a different list of build phases,
wherein a build phase represents a stage in the lifecycle

validate - validate the project is correct and all necessary information is ava
ilable
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing framework. Th
ese tests should not require the code be packaged or deployed
package - take the compiled code and package it in its distributable format, suc
h as a JAR.
integration-test - process and deploy the package if necessary into an environme
nt where integration tests can be run
verify - run any checks to verify the package is valid and meets quality criteri
a
install - install the package into the local repository, for use as a dependency
in other projects locally
deploy - done in an integration or release environment, copies the final package
to the remote repository for sharing with other developers and projects.

mvn clean dependency:copy-dependencies package


If this were to be executed, the clean phase will be executed first (meaning it
will run all preceeding phases of the clean lifecycle, plus the clean phase itse
lf), and then the dependency:copy-dependencies goal, before finally executing th
e package phase (and all its preceeding build phases of the default lifecycle)

Packaging :::

Phases Goals
process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy

As you will see in the later sections, plugins can contain information that indi
cates which lifecycle phase to bind a goal to. Note that adding the plugin on it
s own is not enough information - you must also specify the goals you want to ru
n as part of your build.

<plugin>
<groupId>com.mycompany.example</groupId>
<artifactId>display-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>time</goal>
</goals>
</execution>
</executions>
</plugin>
...

Phase will bind it to the "process-test-resources" phase of the Build life cy


cle and goal time is executed at that time.
if <phase>process-test-resources</phase> is removed from above we need
to specify the plugin:goal name to get it executed.

POM.XML
A Project Object Model or POM is the fundamental unit of work in Maven. It is an
XML file that contains information about the project and configuration details
used by Maven to build the project

Super POM
The Super POM is Maven s default POM. All POMs extend the Super POM unless expli
citly set, meaning the configuration specified in the Super POM is inherited by
the POMs you created for your projects.
Minimal POM
The minimum requirement for a POM are the following:
project root
modelVersion - should be set to 4.0.0
groupId - the id of the project s group.
artifactId - the id of the artifact (project)
version - the version of the artifact under the specified group

A POM requires that its groupId, artifactId, and version be configured. These th
ree values form the project s fully qualified artifact name. This is in the form
of <groupId>:<artifactId>:<version>. As for the example above, its fully qualif
ied artifact name is "com.mycompany.app:my-app:1".

mvn effective:pom -- CLubs project pom with Super pom.


Default packaging type is jar ::

Project Inheritance
Elements in the POM that are merged are the following:
dependencies
developers and contributors
plugin lists (including reports)
plugin executions with matching ids
plugin configuration
resources

<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>

<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>

Project Aggregation ::::

Project Aggregation is similar to Project Inheritance. But instead of specifying


the parent POM from the module, it specifies the modules from the parent POM. B
y doing so, the parent project now knows its modules, and if a Maven command is
invoked against the parent project, that Maven command will then be executed to
the parent s modules as well. To do Project Aggregation, you must do the followi
ng:
Change the parent POMs packaging to the value "pom" .
Specify in the parent POM the directories of its modules (children POMs)

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>my-module</module>
</modules>
</project>
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
How would the parent pom specify its modules?
The Solution
The answer? - the same way as Example 3, by specifying the path to the module.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>../my-module</module>
</modules>
</project>:::

you can have both Project Inheritance and Project Aggregation. Meaning, you can
have your modules specify a parent project, and at the same time, have that pare
nt project specify those Maven projects as its modules. You d just have to apply
all three rules:
Specify in every child POM who their parent POM is.
Change the parent POMs packaging to the value "pom" .
Specify in the parent POM the directories of its modules (children POMs) ::

Project Interpolation and Variables ::


o access the project.version variable, you would reference it like so:
<version>${project.version}</version>

Any field of the model that is a single value element can be referenced as a v
ariable. For example, ${project.groupId}, ${project.version}, ${project.build.so
urceDirectory} and so on
ou are also able to reference any properties defined in the project as a varia
ble. Consider the following example:
ou are also able to reference any properties defined in the project as a varia
ble. Consider the following example:

PROFILES :::
What are the different types of profile? Where is each defined?
Per Project
- Defined in the POM itself (pom.xml).
Per User
- Defined in the Maven-settings (%USER_HOME%/.m2/settings.xml).
Global
- Defined in the global Maven-settings (%M2_HOME%/conf/settings.xml).
Profile descriptor
- a descriptor located in project basedir (profiles.xml) (unsupported in Maven 3
.0: see Maven 3 compatibility notes)

How can a profile be triggered? How does this vary according to the type of prof
ile being used?
A profile can be triggered/activated in several ways:
Explicitly
Through Maven settings
Based on environment variables
OS settings
Present or missing files

Profiles can be explicitly specified using the -P CLI option.


mvn groupId:artifactId:goal -P profile-1,profile-2

Profiles can be activated in the Maven settings, via the <activeProfiles> secti
on. This section takes a list of <activeProfile> elements, each containing a pro
file-id inside.
<settings>
...
<activeProfiles>
<activeProfile>profile-1</activeProfile>
</activeProfiles>
...
</settings>
Profiles listed in the <activeProfiles> tag would be activated by default every
time a project use it.
<profiles>
<profile>
<activation>
<jdk>[1.3,1.6)</jdk>
</activation>
...
</profile>
</profiles>

This next one will activate based on OS settings. See the Maven Enforcer Plugin
for more details about OS values.
<profiles>
<profile>
<activation>
<os>
<name>Windows XP</name>
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
</activation>
...
</profile>
</profiles>
Deactivating a profile
mvn groupId:artifactId:goal -P !profile-1,!profile-2

Which areas of a POM can be customized by each type of profile? Why? :::

mostly build changing profiles can be done in only POM.xml not in settings.xml/p
rofiles.xml as they are not portable

mvn help:active- -- Profiles active during Pom.xml run

Introduction to Repositories ::
Artifact Repositories :::

A repository in Maven is used to hold build artifacts and dependencies of varyin


g types.
There are strictly only two types of repositories: local and remote. The local r
epository refers to a copy on your own installation that is a cache of the remot
e downloads, and also contains the temporary build artifacts that you have not y
et released.
Remote repositories refer to any other type of repository, accessed by a variety
of protocols such as file:// and http://
Downloading in Maven is triggered by a project declaring a dependency that is no
t present in the local repository (
By default, Maven will download from the central repository.
To override this, you need to specify a mirror
This is used to change the default repository tobe searched when downloading dep
endencies :: for uploading artifacts we will use repositories element
<settings>
...
<mirrors>
<mirror>
<id>UK</id>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
...
</settings>
mvn -o package --------Build maven offline without downloading artifacts ::

1180
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>

Dependency Scope :: Default Scop is Compile


compile
This is the default scope, used if none is specified. Compile dependencies are a
vailable in all classpaths of a project. Furthermore, those dependencies are pro
pagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to pr
ovide the dependency at runtime. For example, when building a web application fo
r the Java Enterprise Edition, you would set the dependency on the Servlet API a
nd related Java EE APIs to scope provided because the web container provides tho
se classes. This scope is only available on the compilation and test classpath,
and is not transitive.
runtime
This scope indicates that the dependency is not required for compilation, but is
for execution. It is in the runtime and test classpaths, but not the compile cl
asspath.
test
This scope indicates that the dependency is not required for normal use of the a
pplication, and is only available for the test compilation and execution phases.
system
This scope is similar to provided except that you have to provide the JAR which
contains it explicitly. The artifact is always available and is not looked up in
a repository.
import (only available in Maven 2.0.9 or later)
This scope is only used on a dependency of type pom in the <dependencyManagement
> section. It indicates that the specified POM should be replaced with the depen
dencies in that POM s <dependencyManagement> section. Since they are replaced, d
ependencies with a scope of import do not actually participate in limiting the t
ransitivity of a dependency.

Optionakl dependencies
<project>
...
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
</dependencies>
</project>

Dependency Exclusions ::
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

What is Archetype?
In short, Archetype is a Maven project templating toolkit. An archetype is defin
ed as an original pattern or model from which all other things of the same kind
are made. The name fits as we are trying to provide a system that provides a con
sistent means of generating Maven projects.

Using an Archetype
To create a new project based on an Archetype, you need to call mvn archetype:ge
nerate goal, like the following:
mvn archetype:generate

<distributionmanagement>
<mirrors>
<repositories>
<servers>

Potrebbero piacerti anche