Escolar Documentos
Profissional Documentos
Cultura Documentos
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 archetype?
Archetype is the maven plugin. It creates the project structure.
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/.
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.
Q14. How can we see Dependencies for the project and where exactly they are def
ined ?
Ans. Using
mvn dependency:tree
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.
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>
...
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".
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>
<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) ::
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 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
Introduction to Repositories ::
Artifact Repositories :::
1180
<project>
...
<repositories>
<repository>
<id>my-internal-site</id>
<url>http://myserver/repo</url>
</repository>
</repositories>
...
</project>
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>