Maven Tips and Tricks - 2015

Install Maven AutoCompeletion
brew tap homebrew/completions

brew install maven-completion
Download source jars from maven repository
-DdownloadSources=true -DdownloadJavadocs=true
Maven packaging without test (skip tests)
mvn install -DskipTests
mvn install -Dmaven.test.skip=true
-DskipTests will compile test classes but not run them; -Dmaven.test.skip=true will skip compiling the tests
Maven Repository: Search/Browse/Explore
Maven Available Plugins
Maven Archetypes List
mvn archetype:generate will first ask to choose the archetype from the internal catalog. mvn archetype:create -DartifactId=myApp mvn archetype:create \ -DarchetypeGroupId=<archetype-groupId> \ -DarchetypeArtifactId=<archetype-artifactId> \ -DarchetypeVersion=<archetype-version> \ -DgroupId=<my.groupid> \ -DartifactId=<my-artifactId> mvn archetype:create -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.** -DartifactId=** 

Maven POM Reference
Dependency Classifier
The classifier allows to distinguish artifacts that were built from the same POM but differ in their content. It can be arbitrary string, and is appended to the artifact name just after the version number.
Advantages of having an internal private repository:
Reduced likelihood of version conflicts
It's faster as maven artifacts are retrieved from a server on the intranet rather than from the internet.
It’s quicker to do a clean build.
Hold company's private libraries.
Steps to configure Nexus:
Download Nexus from here
Copy the war to TOMCAT_HOME/webapps/nexus.war
Optional steps:
Configure security
Change default admin password(admin123)
Disable Anonymous Access
Next tell maven to use your maven repository. To do this, modify $HOME/.m2/settings.xml like this.
Project dependencies Analysis
mvn dependency:analyze determines which are: used and declared; used and undeclared; unused and declared. 
Frequently Asked Technical Questions
Maven use JAVA_HOME to specify java path.
How do I prevent "[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!"
<properties> <>UTF-8</> </properties>
How can I use Maven features in an Ant build?
mvn ant:ant generate ant build.xml from existing pom.xml. * ant:ant Generate Ant build files. * ant:clean Clean all Ant build files.
How do I skip the tests? -Dmaven.test.skip=true or -DskipTests=true  
How can I run a single unit test?
mvn -Dtest=TestCircle test mvn -Dtest=TestCi*le test mvn -Dtest=TestSquare,TestCi*le test  
Properties in pom.xml
1. env.X: Prefixing a variable with "env." will return the shell's environment variable: ${env.PATH}.
2. project.x: A dot (.) notated path in the POM will contain the corresponding element's value: ${project.version}, ${}, ${}.
3. settings.x: A dot (.) notated path in the settings.xml will contain the corresponding element's value. For example: <settings><offline>false</offline></settings> is accessible via ${settings.offline}.
4. Java System Properties: All properties accessible via java.lang.System.getProperties() are available as POM properties, such as ${java.home}.
5. x: Set within a <properties /> element. The value may be used as ${someVar}.
How can I tell which profiles are in effect during a build?
mvn help:active-profiles  
Dependency Scope compile, provided, runtime, test, system, import
provided dependencies are only available on the compilation and test classpath and are not packaged
mvn install
mvn install:install-file -Dfile=/path/to/commercial.jar \ -DartifactId=killerapp-commercial \ -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true  
Maven Archiver 
Add A Class-Path Entry To The Manifest and Make The Jar Executable
<plugin> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> ... <configuration>
<archive> <manifest>
<addClasspath>true</addClasspath> <mainClass>fully.qualified.MainClass</mainClass>
... </plugin>  
Set Java Version of Source and Target
Maven Assembly Plugin
Pre-defined Descriptor Files - bin, jar-with-dependencies, src, project
Building an Assembly as a Standalone Process
mvn assembly:assembly mvn assembly:assembly -DdescriptorId=jar-with-dependencies
Building an Assembly as Part of the Build Lifecycle and Creating an Executable Jar
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>essni.diffDoc.MethodPrinter</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <classpathLayoutType>repository</classpathLayoutType> <!-- NOTE: Deprecated in version 2.4. Use 'classpathLayoutType' instead. --> <classpathMavenRepositoryLayout>true</classpathMavenRepositoryLayout> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- append to the packaging phase. --> <goals> <goal>single</goal> <!-- goals == mojos --> </goals> </execution> </executions> </plugin>
Maven increase memory size to avoid OutOfMemoryError
Setting variable: export MAVEN_OPTS=-Xmx1024m
Append the groupId of the artifact to the name of the generated Eclipse project.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <addGroupIdToProjectName> true </addGroupIdToProjectName> </configuration> </plugin> Validte pom - mvn pom:validate
Adding Repositories to Maven 2
Add the following to project's pom.xml file or ~/.m2/settings.xml file.
<repositories> <repository> <id>my-internal-site</id> <url>http://myserver/repo</url> </repository> </repositories>
Differences between Repositories and pluginRepositories
The pluginRepository elements are used to specify a remote location of where Maven can find plugins artifacts.
Add sonatype repository to get latest tycho maven plugin in pom.xml

Code Coverage tools
mvn cobertura:cobertura
Create a appfuse project with Hibernate, Spring and Struts 2
mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-light-struts-archetype -DarchetypeVersion=2.1.0-M1 -DgroupId=com.mycompany -DartifactId=myproject mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts -DarchetypeVersion=2.0.2 -DgroupId=org.appfuse -DartifactId=basic-struts
Create an eclipse project
mvn org.apache.maven.plugins:maven-eclipse-plugin:2.6:eclipse  
Maven - how can I add an arbitrary classpath entry to a jar?
Add a 3rd party library to maven 2 build classpath without installing them Maven 2 assembly with dependencies: jar under scope “system” not included
Post a Comment


Java (159) Lucene-Solr (110) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (36) Eclipse (34) Code Example (31) Linux (24) JavaScript (23) Spring (22) Windows (22) Web Development (20) Nutch2 (18) Tools (18) Bugs (17) Debug (15) Defects (14) Text Mining (14) J2EE (13) Network (13) PowerShell (11) Troubleshooting (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Http Client (8) Maven (8) Problem Solving (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) ANT (6) Coding Skills (6) Database (6) Scala (6) Shell (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) Lesson Learned (5) Programmer Skills (5) Tips (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) System Design (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts