- Maven
Apache Maven is a software project management and comprehension
http://maven.apache.org/
- What is Maven?
Maven is a project management and comprehension
To summarize, Maven simplifies and standardizes the project build process
Maven provides developers ways to manage following:
Builds
Documentation
Reporting
Dependencies
SCMs
Releases
Distribution
mailing list
Maven History
Apache group then developed Maven which can build multiple projects together, publish projects information, deploy projects, share JARs across several projects and help in
Convention over Configuration
Developers do not have to mention
When a Maven project
Maven POM
It is
The POM contains information about the project and various configuration detail used by Maven to build the project
POM also contains the goals and plugins
Before creating a POM, we should first decide the project group (
Projects notation in
Maven already added
C:\MVN\
Maven will start building the project
POM.xml file with following contents.
<project
http://maven.apache.org/xsd/maven-4.0.0.xsd
<
<
<
<version>1.0</version>
<dependencies>
<dependency>
<
<
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
Packaged jar is available in
Test reports are available in
Maven compiled source code file
Then Maven run the test cases.
Finally Maven created the package.
open
C:\MVN\
You will see the result
Hello World!
http://www.tutorialspoint.com/maven/maven_quick_guide.htm
- The differences between Ant and Maven in this example are:
Apache Ant
Ant doesn’t have formal conventions like a common project directory structure or default behavior.
Ant is procedural.
Ant doesn’t have a
Apache Maven
Maven has conventions. It knows where your source code is because you followed the convention. Maven’s Compiler plugin put the
Maven is declarative. All you had to do was create a pom.xml file and put your source in the default directory. Maven took care of the rest.
Maven has a
(1)
The Maven Archetype plugin creates a directory simple/ that matches the
(2)
Every Maven project has
(3)
Our project’s source code and resources
(4)
Our project’s test cases
http://books.sonatype.com/mvnex-book/reference/installation-sect-compare-ant-maven.html
- 1. In
traditional way
Visit http://logging.apache.org/log4j/
Download the Log4j jar library
Copy jar to project
Include it into your project dependency manually
All manages by yourself, you need to do everything
If there is a Log4j version upgrade, you need to repeat above steps again.
2. In Maven way
You need to know the log4j Maven coordinates, for example
<
<
<version>1.2.14</version>
It will download the log4j version 1.2.14 library automatically. If the “version” tag
Declares Maven coordinates into pom.xml file.
<dependencies>
<dependency>
<
<
<version>1.2.14</version>
</dependency>
</dependencies>
When Maven is compiling or building, the log4j jar will
All manages by Maven.
http://www.mkyong.com/maven/how-to-use-maven-dependency-to-download-library-automatically/
- Maven Build Life Cycle
Phase Handles Description
prepare-resources resource copying Resource copying can
compile
package packaging This phase creates the JAR / WAR package as mentioned in packaging in POM.xml.
install installation This phase installs the package in local / remote maven repository.
http://www.tutorialspoint.com/maven/maven_quick_guide.htm
- Generating Maven Java project compatible with Eclipse
To make this project compatible with eclipse
http://www.tuicool.com/articles/3QBJnu
- "
pluginManagement "
Within the pom.xml file of the parent project, we use the
The
Like "
http://java-success.blogspot.com/2011/10/maven-interview-questions-and-answers.html
- Maven Build Profiles
A Build profile is a set of configuration values which can
Using a build profile, you can customize build for different environments such as Production v/s Development environments.
Profiles
Types of Build Profile
Per Project Defined in the project POM file, pom.xml
Per User Defined in Maven settings xml file (
Global Defined in Maven global settings xml file (
http://www.tutorialspoint.com/maven/maven_build_profiles.htm
- A profile in Maven is an alternative set of configuration values which set or override default values.
Using a profile, you can customize a build for different environments.
Profiles
you can run Maven with a command-line flag that tells Maven to execute goals in a specific profile
a profile named production that overrides the default configuration of the Maven Compiler plugin
<profiles> (1)
<profile>
<
<build> (3)
<plugins>
<plugin>
<
<
<configuration>
<
<
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
1
The profiles element is in the pom.xml, it contains one or more profile elements.
Since profiles override the default settings in a pom.xml,
2
Each profile has to have an id element. This id element contains the name which
3
we’re overriding the behavior of the Compiler plugin and we have to override the plugin configuration which
$
you still don’t know exactly what
What happens when you need to provide customizations based on variables
Maven provides a way to "activate" a profile for different environmental parameters, this
http://books.sonatype.com/mvnref-book/reference/profiles-sect-maven-profiles.html
- Don’t use deprecated references like ${
artifactId } or ${pom. artifactId }. Use the new ${project. artifactId } syntax.
Try to avoid using inherited properties. Developers can easily forget that a certain property
Use properties to define the dependency versions. This way you can get an overview of all versions being used without having to scroll through multiple pages of dependency sections.
Use the
When using a parent POM that
Use the dependency plugin to check your project for both unnecessary dependencies and undeclared-but-used-none-the-
Make sure the
Make sure the
http://www.javacodegeeks.com/2012/06/maven-best-practices.html
- Maven Continuous Integration Best Practices
Here are seven tips for running Maven builds in a CI system such as Hudson.
#1 Automate Snapshot Deployment
it is best to let your CI system deploy your snapshots.
you need to couple CI with a repository manager like Nexus that can automatically purge snapshots.
#2 Isolate Local
The main reason I like to have a local repository per project is that it’s the only way to test that your project
#3 Regularly Purge Local Repositories
To further validate the contents of the repository, and to manage the disk space, I purge the local
#4 Enable Batch Mode: maven -B
Tip: Enable -B (
#5 Enable Full Stack Traces
Tip: Enable -e to cause Maven to produce the full stack trace if there’s a build exception
#7 Always check for Snapshots
Tip: Enable -U to cause Maven to always check for new snapshots. (to enable globally in settings.xml: <
http://blog.sonatype.com/people/2009/01/maven-continuous-integration-best-practices/#
- POM Best Practices
3.6.1. Grouping Dependencies
For example, let’s assume that your application uses Hibernate, a popular Object-Relational mapping framework.
Every project which uses Hibernate might also have a dependency on the Spring Framework and a MySQL JDBC driver.
Instead of having to include these dependencies in every project that uses Hibernate, Spring, and MySQL you could create a special POM that does nothing more than declare a set of common dependencies.
You could create a project called persistence-
If you create this project in a directory named persistence-
Since the packaging type is
When you declare a dependency on this persistence-
<dependency>
<
<
<version>1.0</version>
<
</dependency>
If you later
3.6.2. Multi-module vs. Inheritance
There is a difference between inheriting from a parent project and being managed by a
A parent project is one that passes its values to its children.
A
The
When setting up a
The parent-child relationship
http://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-pom-best-practice.html#pom-relationships-sect-grouping-deps
- Maven's command-line options
-B - runs Maven in batch mode, which is
-e - configures Maven to report detailed information about errors, which is
The target folder
Maven's
For example, suppose a multi-module Java project
Dependency Management in parent pom.xml
<
<dependencies>
<dependency>
<
<
<version>4.8.1</version>
<
</dependency>
</dependencies>
</
Note that each child project that uses
Dependency in child pom.xml (version and scope inherited from
<dependencies>
<dependency>
<
<
</dependency>
</dependencies>
The Maven Dependency Plugin has an analyze goal that identifies two types of dependency issues in a project:
Dependencies that
Dependencies that
http://www.kyleblaney.com/maven-best-practices/
- Maven Profile Best Practices
One way to activate a profile is
they can also
http://java.dzone.com/articles/maven-profile-best-practices
- When setting up a maven project
consisting of multiplemodules there are multiple ways to define the project dependencies.
1- Use
2- Use
3- Use
Approach 3
Plus Side:
Parent POM is easily
Good integration with continuous integration systems because module definitions do not interfere with standalone module builds.
Minus Side:
POMs containing module definitions may be difficult to integrate in IDE.
Especially the compatibility with both IDEs and continuous build systems like Hudson make your work a lot easier.
http://maventweaks.blogspot.com/2010/10/best-practices-for-multi-module-project.html
- Maven: The Aggregator vs. The Parent
A top-level module serving for joining multiple modules under one roof
Its purpose is only represent more or less independently existing modules as
<project
<
<
<
<
<version>1.0</version>
<
<modules>
<module>project1</module>
<module>project2</module>
</modules>
</project>
Parent POM
aggregator does not include any information about dependencies
Parent POM includes all the properties,
http://rostislav-matl.blogspot.com/2011/12/maven-aggregator-vs-parent.html
- we create a multi-module project that combines the examples simple-weather code and simple-
webapp . A multi-module projectis defined by a parent POM referencing one or moresubmodules .
http://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-parent.html
Dependency scope is used to limit the transitivity of a dependency, and also to affect theclasspath used for various build tasks.
<scope>compile </scope>
This is the default scope, used if none is specified . Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects
Compile means you need the JAR for compiling and running. For a web application, JAR will be placed in the WEB-INF/lib directory
<scope>provided </scope>
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment. Mostly environment is container and JAR will not be placed under WEB-INF/lib
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
versions:display -property-updates scans aprojectand produces a report of those properties whichare used to control artifact versions and whichproperies have newer versions available.
http://mojo.codehaus.org/versions-maven-plugin/
- Maven Jetty Plugin Configuration Guide
This will start Jetty running on port 8080 and serving your project.
It is extremely convenient to leave the plugin running because it can be configured to periodically scan for changes and automatically redeploy the webapp .
This makes the development cycle much more productive by eliminating the build and deploy steps:
you use your IDE to make changes to the project and the running web container will automatically pick them up, allowing you to test them straight away.
- Finding duplicate classes in your WAR files with Tattletale
http://blog.csanchez.org/2011/03/23/finding-duplicate-classes-in-your-war-files-with-tattletale/
- Tattletale is a tool that can help you get an overview of the project you are working on or a product
that you depend on.
http://tattletale.jboss.org/
JBoss Tattletale integrates with Apache Maven such that you can generate the reports directly from your build environment.
To be able to use the Tattletale Maven plugin in your Maven project, you will have to add the following plugin declaration in the pom.xml of your project:
http://docs.jboss.org/tattletale/userguide/1.2/en-US/html/maven.html
- Have you ever found
all sorts of weird errors when running yourwebapp because several jar files included have the same classes in different versions and the wrong one is being picked up by the application server?
http://java.dzone.com/articles/finding-duplicate-classes-your
- There are two locations where
.xml file may livea settings
The Maven install: $M2_HOME/conf/settings.xml also called global settings
A user's install: ${user. home}/. m2/settings.xml referred to as user settings.
If both files exists , their contents gets merged, with the user-specific settings.xml being dominant.
Tip: If you need to create user-specific settings from scratch, it's easiest to copy the global settings from your Maven installation to your ${user. home}/. m2 directory. Maven's default settings.xml is a template with comments and examples so you can quickly tweak it to match your needs.
https://maven.apache.org/settings.html#Servers
Builds were typically done straight from the developer’s IDE and manually deployed to one of our app servers. We had a manual process in place , where the developer would do the following steps.
Check all project code into Subversion and tag
Build the application.
Archive the application binary to a network drive
Deploy to production
Update our deployment wiki with the date and version number of the app that was just deployed.
The problem is that there were occasionally times where one of these steps were missed, and it always seemed to be at a time when we needed to either rollback to the previous version, or branch from the tag to do a bugfix . Sometimes the previous version had not been archived to the network, or the developer forgot to tag SVN. We were already using Jenkins to perform automated builds, so we wanted to look at extending it further to perform release builds.
The Maven Release plug-in provides a good starting point for creating an automated release process.
We have also just started using the Nexus Maven repository and wanted to incorporate that as well to archive our binaries to, rather than archiving them to a network drive.
http://rterp.wordpress.com /tag/build-pipeline/
- The Maven Release Cycle
Deploying a release involves many tasks
Commit all current changes to version control
Upgrade the version number( s) and commit the changes
Tag the release version
Build and deploy this version
Update the version number and commit the changes
Building your release process on solid building blocks:
The Maven Enterprise Repository
The Maven Release Cycle
Deploying a release involves many tasks
Commit all current changes to version control
Upgrade the version number( s) and commit the changes
Tag the release version
Build and deploy this version
Update the version number and commit the changes
The Maven Release Plugin automates this process
Automates the release process
Updates version numbers
Commits changes and creates new SCM tags
Builds, tests and deploys to the Enterprise Repository
- Apache Tomcat Maven Plugin
to run your war project with an embedded Apache Tomcat. The run goals give you the opportunity to quickly develop your application without needing to install a standalone Tomcat instance. More details and features
http://tomcat.apache.org/maven-plugin-2.1/index.html
The POM was renamed fromproject .xml in Maven 1 to pom.xml in Maven 2. Instead of having a maven.xml file that contains the goals that canbe executed , the goals or pluginsare now configured in the pom.xml. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.
http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
- there is one aspect of Maven-based development that's potentially troublesome: the reliance on a remote central repository somewhere on the Internet.
When a new developer joins a team, every single dependency is copied from the remote repository to his or her local repository cache
This places an unnecessarily large load on the company's or individual's Internet link for the duration of the dependency download. Even worse, should the Internet link go down, the developer is unable to work.
So what's the answer to all that wasted Internet bandwidth? A local Maven proxy. The first time a developer requests a dependency, it is copied from the central repository to the local repository, which resides on the machine running the proxy. Subsequent requests by developers in the team are then fed the copy from the local repository, typically on the same local area network (LAN). That translates to quicker builds and — as long as no new dependencies are needed — no reliance on the corporate Internet link for builds.
http://www.openlogic.com/wazi/bid/188151/
- Maven Archetypes are project templates that allow users easily create new projects. Maven Archetypes are
great way to share best practices and enforce consistency beyond Maven’s standard directory structure. For example, an organization can provide its developers an archetype bundled withcompany ’s approved CSS and JavaScript libraries.
I have been creating a lot of Spring MVC based projects and each time I do, I copy a lot of information (configuration files, css files, dependency information etc) from an existing project. Following the DRY principles, I decided to create several Spring MVC based archetypes that can help me bootstrap my projects quickly
http://blog.inflinx.com/2010/04/16/creating-maven-archetypes-spring-mvc-example/
- Using the Build Cache for Apache Maven
While Maven does not provide support for incremental builds, the
Thus, it avoids executing costly goals and
The remote build cache takes this even one step further: it allows you to share cached outputs across your whole team, including local and CI builds.
https://docs.gradle.com/enterprise/maven-build-cache/
- Remote Caching
A remote cache
https://docs.bazel.build/versions/master/remote-caching.html
- Differences between Maven and
Bazel
Maven uses
Maven takes charge of steps for the deployment process.
As you add new sections to the project, with
https://docs.bazel.build/versions/master/migrate-maven.html
Thanks for this blog post admin, you have done really nice work.
ReplyDeletevision inspection system manufacturers