24 April 2020

Maven Fundamentals (Course on Plural Sight):

Maven

  • Works with Eclipse/ net beans.
  • Preferred choice for working with build tools like Cruise Control/Bamboo.
  • Ant is build on top of Java and XML in procedural way. Ant developed to be replacement for unix build tool Make. You need to define everything you want to do like clean/clear/cleanup etc. No inheritance, no reuse. Ant is scripting tool.
  • using Maven it is full fledged build tool.
  • transitive dependencies and capable to achieve inheritance in projects. In Maven we have pom.xml unlike build.xml in ant. Maven is interested in managing entire project life cycle.
  • Copy apach-maven-3.5.0 and add bin directory of maven to Path environment variable. run below command to know the version of maven
    mvn -version
  • Create a General project in STS and name it TestCalculator and add a file to the project with name pom.xml (project object model). and create a java file in that with some text printing on to the console.
    contents of pom.xml
<project>
<groupId>com.pluralsight</groupId>
<artifactId>TestCalculator</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

</project>
  • now navigate to the workspace folder from command prompt and run
mvn clean
mvn compile
then
go to
cd target/classes
and run below command to print output Madhu
java TestCalculator
  • run below command to create jar file as per pom.xml
    mvn package
all java code goes to src/main/java directory All test code is in src/test/java . All other language code is in src/main/groovy directory. All classes after compiling goes to target/classes/

some maven commands useful:

mvn clean package
mvn install
this will run package command and move the .jar to the C:\users\talakokm.m2\repository in that with group name set in pom.xml
even dependencies are copied here

Goals

clean
compile: compile src/main directory and move the classes to target/classes.
package: create jar/ear/war
install:Run package command and installs to local repository.
deploy: run install command and deploys to corporate repository.

pom.xml contents

Project is of 4 parts
groupId: is used in packaging of application like com.cdk
artifaceId: is application name.
version: version number of application.
packaging: is to mention how we want to package the application for example .war/.ear/.jar.
Dependencies
transitive dependencies will be pulled by Maven , it needs 3 things
groupId
artifactId
version
Example:
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang<artifactId>
<version>2.1</version>
</dependency>
</dependencies>
Add below build tag to pom.xml to set final name of our application while building application:
<build>
<finalName>foo</finalName>
</build>
now foo.jar is created in target directory with name foo.jar

Transitive dependencies:

Is the main reason why we use Maven.
Example: Add below dependency it will download all transitive dependencies.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<versions>4.1.6.Final</version>
<scope>compile</scope>
</dependency>

Scope types in dependency:
There are six types of scopes available in dependencies.
compile - default scope. available everywhere
provide - like compile. available everywhere but not included in final artifact.
runtime - available only in runtime.
test - only available for test compilation/execution phase.
SNAPSHOT has special meaning. every compile or run picks up latest code.

Dependency Repository:

is where we store/download all our dependencies
specifying our own repository
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Maven SNAPSHOT Repository</name>
<url>http://repo.springsource.org/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

Convert project to Maven Project via STS

in STS , select the project and right click and select configure --> convert to Maven project.

Adding dependencies:

using pom viewer we can edit pom.xml
also using dependencies tab we can update/add dependencies. dependency hierarchy automatically updated after save.

Effective POM

there is tab called Effective POM on pom.xml useful for debugging pom.xml to know what pom is doing.

No comments:

Post a Comment