Maven – Deployment Automation ”; Previous Next In project development, normally a deployment process consists of the following steps − Check-in the code from all project in progress into the SVN (version control system) or source code repository and tag it. Download the complete source code from SVN. Build the application. Store the build output either WAR or EAR file to a common network location. Get the file from network and deploy the file to the production site. Updated the documentation with date and updated version number of the application. Problem Statement There are normally multiple people involved in the above mentioned deployment process. One team may handle check-in of code, other may handle build and so on. It is very likely that any step may be missed out due to manual efforts involved and owing to multi-team environment. For example, older build may not be replaced on network machine and deployment team deployed the older build again. Solution Automate the deployment process by combining the following − Maven, to build and release projects. SubVersion, source code repository, to manage source code. Remote Repository Manager (Jfrog/Nexus) to manage project binaries. Update Project POM.xml We will be using Maven Release plug-in to create an automated release process. For Example: bus-core-api project POM.xml. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <scm> <url>http://www.svn.com</url> <connection>scm:svn:http://localhost:8080/svn/jrepo/trunk/ Framework</connection> <developerConnection>scm:svn:${username}/${password}@localhost:8080: common_core_api:1101:code</developerConnection> </scm> <distributionManagement> <repository> <id>Core-API-Java-Release</id> <name>Release repository</name> <url>http://localhost:8081/nexus/content/repositories/ Core-Api-Release</url> </repository> </distributionManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.0-beta-9</version> <configuration> <useReleaseProfile>false</useReleaseProfile> <goals>deploy</goals> <scmCommentPrefix>[bus-core-api-release-checkin]-< /scmCommentPrefix> </configuration> </plugin> </plugins> </build> </project> In Pom.xml, following are the important elements we have used − Sr.No. Element & Description 1 SCM Configures the SVN location from where Maven will check out the source code. 2 Repositories Location where built WAR/EAR/JAR or any other artifact will be stored after code build is successful. 3 Plugin maven-release-plugin is configured to automate the deployment process. Maven Release Plug-in The Maven does the following useful tasks using maven-release-plugin. mvn release:clean It cleans the workspace in case the last release process was not successful. mvn release:rollback Rollback the changes done to workspace code and configuration in case the last release process was not successful. mvn release:prepare Performs multiple number of operations, such as − Checks whether there are any uncommitted local changes or not. Ensures that there are no SNAPSHOT dependencies. Changes the version of the application and removes SNAPSHOT from the version to make release. Update pom files to SVN. Run test cases. Commit the modified POM files. Tag the code in subversion Increment the version number and append SNAPSHOT for future release. Commit the modified POM files to SVN. mvn release:perform Checks out the code using the previously defined tag and run the Maven deploy goal, to deploy the war or built artifact to repository. Let”s open the command console, go to the C: > MVN >bus-core-api directory and execute the following mvn command. >mvn release:prepare Maven will start building the project. Once build is successful run the following mvn command. >mvn release:perform Once build is successful you can verify the uploaded JAR file in your repository. Print Page Previous Next Advertisements ”;
Category: maven
Maven – Build Automation
Maven – Build Automation ”; Previous Next Build Automation defines the scenario where dependent project(s) build process gets started once the project build is successfully completed, in order to ensure that dependent project(s) is/are stable. Example Consider a team is developing a project bus-core-api on which two other projects app-web-ui and app-desktop-ui are dependent. app-web-ui project is using 1.0-SNAPSHOT of bus-core-api project. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>app-web-ui</groupId> <artifactId>app-web-ui</artifactId> <version>1.0</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project> app-desktop-ui project is using 1.0-SNAPSHOT of bus-core-api project. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>app_desktop_ui</groupId> <artifactId>app_desktop_ui</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>app_desktop_ui</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>bus_core_api</groupId> <artifactId>bus_core_api</artifactId> <version>1.0-SNAPSHOT</version> <scope>system</scope> <systemPath>C:MVNbus_core_apitargetbus_core_api-1.0-SNAPSHOT.jar</systemPath> </dependency> </dependencies> </project> bus-core-api project − <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>bus_core_api</groupId> <artifactId>bus_core_api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> </project> Now, teams of app-web-ui and app-desktop-ui projects require that their build process should kick off whenever bus-core-api project changes. Using snapshot, ensures that the latest bus-core-api project should be used but to meet the above requirement we need to do something extra. We can proceed with the following two ways − Add a post-build goal in bus-core-api pom to kick-off app-web-ui and app-desktop-ui builds. Use a Continuous Integration (CI) Server like Hudson to manage build automation automatically. Using Maven Update bus-core-api project pom.xml. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>bus-core-api</groupId> <artifactId>bus-core-api</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <build> <plugins> <plugin> <artifactId>maven-invoker-plugin</artifactId> <version>1.6</version> <configuration> <debug>true</debug> <pomIncludes> <pomInclude>app-web-ui/pom.xml</pomInclude> <pomInclude>app-desktop-ui/pom.xml</pomInclude> </pomIncludes> </configuration> <executions> <execution> <id>build</id> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> <build> </project> Let”s open the command console, go to the C: > MVN > bus-core-api directory and execute the following mvn command. >mvn clean package -U Maven will start building the project bus-core-api. [INFO] Scanning for projects… [INFO] —————————————————————— [INFO] Building bus-core-api [INFO] task-segment: [clean, package] [INFO] —————————————————————— … [INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: C:MVNbus-core-uitarget bus-core-ui-1.0-SNAPSHOT.jar [INFO] —————————————————————— [INFO] BUILD SUCCESSFUL [INFO] —————————————————————— Once bus-core-api build is successful, Maven will start building the app-web-ui project. [INFO] —————————————————————— [INFO] Building app-web-ui [INFO] task-segment: [package] [INFO] —————————————————————— … [INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: C:MVNapp-web-uitarget app-web-ui-1.0-SNAPSHOT.jar [INFO] —————————————————————— [INFO] BUILD SUCCESSFUL [INFO] —————————————————————— Once app-web-ui build is successful, Maven will start building the app-desktop-ui project. [INFO] —————————————————————— [INFO] Building app-desktop-ui [INFO] task-segment: [package] [INFO] —————————————————————— … [INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: C:MVNapp-desktop-uitarget app-desktop-ui-1.0-SNAPSHOT.jar [INFO] ——————————————————————- [INFO] BUILD SUCCESSFUL [INFO] ——————————————————————- Using Continuous Integration Service with Maven Using a CI Server is more preferable to developers. It is not required to update the bus-core-api project, every time a new project (for example, app-mobile-ui) is added, as dependent project on bus-core-api project. Hudsion is a continuous integration tool written in java, which in a servlet container, such as, Apache tomcat and glassfish application server. Hudson automatically manages build automation using Maven dependency management. The following snapshot will define the role of Hudson tool. Hudson considers each project build as job. Once a project code is checked-in to SVN (or any Source Management Tool mapped to Hudson), Hudson starts its build job and once this job gets completed, it start other dependent jobs (other dependent projects) automatically. In the above example, when bus-core-ui source code is updated in SVN, Hudson starts its build. Once build is successful, Hudson looks for dependent projects automatically, and starts building app-web-ui and app-desktop-ui projects. Print Page Previous Next Advertisements ”;
Maven – Discussion
Discuss Maven ”; Previous Next Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project”s build, reporting and documentation from a central piece of information. Using maven we can build and manage any Java based project. This tutorial will teach you how to use Maven in your day-to-day life of any project development using Java. Print Page Previous Next Advertisements ”;
Maven – Questions and Answers ”; Previous Next Maven Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations. SN Question/Answers Type 1 Maven Interview Questions This section provides a huge collection of Maven Interview Questions with their answers hidden in a box to challenge you to have a go at them before discovering the correct answer. 2 Maven Online Quiz This section provides a great collection of Maven Multiple Choice Questions (MCQs) on a single page along with their correct answers and explanation. If you select the right option, it turns green; else red. 3 Maven Online Test If you are preparing to appear for a Java and Maven related certification exam, then this section is a must for you. This section simulates a real online test along with a given timer which challenges you to complete the test within a given time-frame. Finally you can check your overall test score and how you fared among millions of other candidates who attended this online test. 4 Maven Mock Test This section provides various mock tests that you can download at your local machine and solve offline. Every mock test is supplied with a mock test key to let you verify the final score and grade yourself. Print Page Previous Next Advertisements ”;
Maven – IntelliJ IDEA
Maven – IntelliJ IDEA IDE Integration ”; Previous Next IntelliJ IDEA has in-built support for Maven. We are using IntelliJ IDEA Community Edition 11.1 in this example. Some of the features of IntelliJ IDEA are listed below − You can run Maven goals from IntelliJ IDEA. You can view the output of Maven commands inside the IntelliJ IDEA using its own console. You can update maven dependencies within IDE. You can Launch Maven builds from within IntelliJ IDEA. IntelliJ IDEA does the dependency management automatically based on Maven”s pom.xml. IntelliJ IDEA resolves Maven dependencies from its workspace without installing to local Maven repository (requires dependency project be in same workspace). IntelliJ IDEA automatically downloads the required dependencies and sources from the remote Maven repositories. IntelliJ IDEA provides wizards for creating new Maven projects, pom.xml. Following example will help you to leverage benefits of integrating IntelliJ IDEA and Maven. Create a new project in IntelliJ IDEA We will import Maven project using New Project Wizard. Open IntelliJ IDEA. Select File Menu > New Project Option. Select import project from existing model. Select Maven option Select Project location, where a project was created using Maven. We have created a Java Project consumerBanking. Go to ‘Creating Java Project” chapter, to see how to create a project using Maven. Select Maven project to import. Enter name of the project and click finish. Now, you can see the maven project in IntelliJ IDEA. Have a look at consumerBanking project external libraries. You can see that IntelliJ IDEA has added Maven dependencies to its build path under Maven section. Build a maven project in IntelliJ IDEA Now, it is time to build this project using capability of IntelliJ IDEA. Select consumerBanking project. Select Buid menu > Rebuild Project Option You can see the output in IntelliJ IDEA Console 4:01:56 PM Compilation completed successfully Run Application in IntelliJ IDEA Select consumerBanking project. Right click on App.java to open context menu. select Run App.main() You will see the result in IntelliJ IDEA Console. “C:Program FilesJavajdk1.6.0_21binjava” -Didea.launcher.port=7533 “-Didea.launcher.bin.path= C:Program FilesJetBrainsIntelliJ IDEA Community Edition 11.1.2bin” -Dfile.encoding=UTF-8 -classpath “C:Program FilesJavajdk1.6.0_21jrelibcharsets.jar; C:Program FilesJavajdk1.6.0_21jrelibdeploy.jar; C:Program FilesJavajdk1.6.0_21jrelibjavaws.jar; C:Program FilesJavajdk1.6.0_21jrelibjce.jar; C:Program FilesJavajdk1.6.0_21jrelibjsse.jar; C:Program FilesJavajdk1.6.0_21jrelibmanagement-agent.jar; C:Program FilesJavajdk1.6.0_21jrelibplugin.jar; C:Program FilesJavajdk1.6.0_21jrelibresources.jar; C:Program FilesJavajdk1.6.0_21jrelibrt.jar; C:Program FilesJavajdk1.6.0_21jrelibextdnsns.jar; C:Program FilesJavajdk1.6.0_21jrelibextlocaledata.jar; C:Program FilesJavajdk1.6.0_21jrelibextsunjce_provider.jar; C:Program FilesJavajdk1.6.0_21jrelibextsunmscapi.jar; C:Program FilesJavajdk1.6.0_21jrelibextsunpkcs11.jar C:MVNconsumerBankingtargetclasses; C:Program FilesJetBrains IntelliJ IDEA Community Edition 11.1.2libidea_rt.jar” com.intellij.rt.execution.application.AppMain com.companyname.bank.App Hello World! Process finished with exit code 0 Print Page Previous Next Advertisements ”;
Maven – Build Life Cycle
Maven – Build Life Cycle ”; Previous Next What is Build Lifecycle? A Build Lifecycle is a well-defined sequence of phases, which define the order in which the goals are to be executed. Here phase represents a stage in life cycle. As an example, a typical Maven Build Lifecycle consists of the following sequence of phases. Phase Handles Description prepare-resources resource copying Resource copying can be customized in this phase. validate Validating the information Validates if the project is correct and if all necessary information is available. compile compilation Source code compilation is done in this phase. Test Testing Tests the compiled source code suitable for testing framework. package packaging This phase creates the JAR/WAR package as mentioned in the packaging in POM.xml. install installation This phase installs the package in local/remote maven repository. Deploy Deploying Copies the final package to the remote repository. There are always pre and post phases to register goals, which must run prior to, or after a particular phase. When Maven starts building a project, it steps through a defined sequence of phases and executes goals, which are registered with each phase. Maven has the following three standard lifecycles − clean default(or build) site A goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation. The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked. For example, consider the command below. The clean and package arguments are build phases while the dependency:copy-dependencies is a goal. mvn clean dependency:copy-dependencies package Here the clean phase will be executed first, followed by the dependency:copy-dependencies goal, and finally package phase will be executed. Clean Lifecycle When we execute mvn post-clean command, Maven invokes the clean lifecycle consisting of the following phases. pre-clean clean post-clean Maven clean goal (clean:clean) is bound to the clean phase in the clean lifecycle. Its clean:cleangoal deletes the output of a build by deleting the build directory. Thus, when mvn clean command executes, Maven deletes the build directory. We can customize this behavior by mentioning goals in any of the above phases of clean life cycle. In the following example, We”ll attach maven-antrun-plugin:run goal to the pre-clean, clean, and post-clean phases. This will allow us to echo text messages displaying the phases of the clean lifecycle. We”ve created a pom.xml in C:MVNproject folder. <project xmlns = “http://maven.apache.org/POM/4.0.0″ xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.pre-clean</id> <phase>pre-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>pre-clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> <execution> <id>id.post-clean</id> <phase>post-clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>post-clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> Now open command console, go to the folder containing pom.xml and execute the following mvn command. C:MVNproject>mvn post-clean Maven will start processing and displaying all the phases of clean life cycle. C:MVN>mvn post-clean [INFO] Scanning for projects… [INFO] [INFO] —————-< com.companyname.projectgroup:project >—————- [INFO] Building project 1.0 [INFO] ——————————–[ jar ]——————————— [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.pre-clean) @ project — [INFO] Executing tasks [echo] pre-clean phase [INFO] Executed tasks [INFO] [INFO] — maven-clean-plugin:2.5:clean (default-clean) @ project — [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.clean) @ project — [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.post-clean) @ project — [INFO] Executing tasks [echo] post-clean phase [INFO] Executed tasks [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 0.740 s [INFO] Finished at: 2021-12-10T20:03:53+05:30 [INFO] ———————————————————————— C:MVN> You can try tuning mvn clean command, which will display pre-clean and clean. Nothing will be executed for post-clean phase. Default (or Build) Lifecycle This is the primary life cycle of Maven and is used to build the application. It has the following 21 phases. Sr.No. Lifecycle Phase & Description 1 validate Validates whether project is correct and all necessary information is available to complete the build process. 2 initialize Initializes build state, for example set properties. 3 generate-sources Generate any source code to be included in compilation phase. 4 process-sources Process the source code, for example, filter any value. 5 generate-resources Generate resources to be included in the package. 6 process-resources Copy and process the resources into the destination directory, ready for packaging phase. 7 compile Compile the source code of the project. 8 process-classes Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes. 9 generate-test-sources Generate any test source code to be included in compilation phase. 10 process-test-sources Process the test source code, for example, filter any values. 11 test-compile Compile the test source code into the test destination directory. 12 process-test-classes Process the generated files from test code file compilation. 13 test Run tests using a suitable unit testing framework (Junit is one). 14 prepare-package Perform any operations necessary to prepare a package before
Maven – Environment Setup
Maven – Environment Setup ”; Previous Next Step 1 – Verify Java Installation in Your Machine First of all, open the console and execute a java command based on the operating system you are working on. OS Task Command Windows Open Command Console c:> java -version Linux Open Command Terminal $ java -version Mac Open Terminal machine:< joseph$ java -version Let”s verify the output for all the operating systems − OS Output Windows java 11.0.11 2021-04-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode) Linux java 11.0.11 2021-04-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode) Mac java 11.0.11 2021-04-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode) If you do not have Java installed on your system, then download the Java Software Development Kit (SDK) from the following link http://www.oracle.com. We are assuming Java 11.0.11 as the installed version for this tutorial. Step 2 – Set JAVA Environment Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example. OS Output Windows Set the environment variable JAVA_HOME to C:Program FilesJavajdk11.0.11 Linux export JAVA_HOME = /usr/local/java-current Mac export JAVA_HOME = /Library/Java/Home Append Java compiler location to the System Path. OS Output Windows Append the string C:Program FilesJavajdk11.0.11bin at the end of the system variable, Path. Linux export PATH = $PATH:$JAVA_HOME/bin/ Mac not required Verify Java installation using the command java -version as explained above. Step 3 – Download Maven Archive Download Maven 3.8.4 from https://maven.apache.org/download.cgi. OS Archive name Windows apache-maven-3.8.4-bin.zip Linux apache-maven-3.8.4-bin.tar.gz Mac apache-maven-3.8.4-bin.tar.gz Step 4 – Extract the Maven Archive Extract the archive, to the directory you wish to install Maven 3.8.4. The subdirectory apache-maven-3.8.4 will be created from the archive. OS Location (can be different based on your installation) Windows C:Program FilesApache Software Foundationapache-maven-3.8.4 Linux /usr/local/apache-maven Mac /usr/local/apache-maven Step 5 – Set Maven Environment Variables Add M2_HOME, M2, MAVEN_OPTS to environment variables. OS Output Windows Set the environment variables using system properties. M2_HOME=C:Program FilesApache Software Foundationapache-maven-3.8.4 M2=%M2_HOME%bin MAVEN_OPTS=-Xms256m -Xmx512m Linux Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m Mac Open command terminal and set environment variables. export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4 export M2=$M2_HOME/bin export MAVEN_OPTS=-Xms256m -Xmx512m Step 6 – Add Maven bin Directory Location to System Path Now append M2 variable to System Path. OS Output Windows Append the string ;%M2% to the end of the system variable, Path. Linux export PATH=$M2:$PATH Mac export PATH=$M2:$PATH Step 7 – Verify Maven Installation Now open console and execute the following mvn command. OS Task Command Windows Open Command Console c:> mvn –version Linux Open Command Terminal $ mvn –version Mac Open Terminal machine:~ joseph$ mvn –version Finally, verify the output of the above commands, which should be as follows − OS Output Windows Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Maven home: C:Program FilesApache Software Foundationapache-maven-3.8.4 Java version: 11.0.11, vendor: Oracle Corporation, runtime: C:Program FilesJavajdk11.0.11 Default locale: en_IN, platform encoding: Cp1252 OS name: “windows 10”, version: “10.0”, arch: “amd64”, family: “windows” Linux Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Java version: 11.0.11 Java home: /usr/local/java-current/jre Mac Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537) Java version: 11.0.11 Java home: /Library/Java/Home/jre Print Page Previous Next Advertisements ”;
Maven – Plug-ins
Maven – Plugins ”; Previous Next What are Maven Plugins? Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally used to − create jar file create war file compile code files unit testing of code create project documentation create project reports A plugin generally provides a set of goals, which can be executed using the following syntax − mvn [plugin-name]:[goal-name] For example, a Java project can be compiled with the maven-compiler-plugin”s compile-goal by running the following command. mvn compiler:compile Plugin Types Maven provided the following two types of Plugins − Sr.No. Type & Description 1 Build plugins They execute during the build process and should be configured in the <build/> element of pom.xml. 2 Reporting plugins They execute during the site generation process and they should be configured in the <reporting/> element of the pom.xml. Following is the list of few common plugins − Sr.No. Plugin & Description 1 clean Cleans up target after the build. Deletes the target directory. 2 compiler Compiles Java source files. 3 surefire Runs the JUnit unit tests. Creates test reports. 4 jar Builds a JAR file from the current project. 5 war Builds a WAR file from the current project. 6 javadoc Generates Javadoc for the project. 7 antrun Runs a set of ant tasks from any phase mentioned of the build. Example We”ve used maven-antrun-plugin extensively in our examples to print data on console. Refer Build Profiles chapter. Let us understand it in a better way and create a pom.xml in C:MVNproject folder. <project xmlns = “http://maven.apache.org/POM/4.0.0″ xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.companyname.projectgroup</groupId> <artifactId>project</artifactId> <version>1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <id>id.clean</id> <phase>clean</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>clean phase</echo> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> Next, open the command console and go to the folder containing pom.xml and execute the following mvn command. C:MVNproject>mvn clean Maven will start processing and displaying the clean phase of clean life cycle. C:MVN>mvn clean [INFO] Scanning for projects… [INFO] [INFO] —————-< com.companyname.projectgroup:project >—————- [INFO] Building project 1.0 [INFO] ——————————–[ jar ]——————————— [INFO] [INFO] — maven-clean-plugin:2.5:clean (default-clean) @ project — [INFO] Deleting C:MVNtarget [INFO] [INFO] — maven-antrun-plugin:1.1:run (id.clean) @ project — [INFO] Executing tasks [echo] clean phase [INFO] Executed tasks [INFO] ———————————————————————— [INFO] BUILD SUCCESS [INFO] ———————————————————————— [INFO] Total time: 1.266 s [INFO] Finished at: 2021-12-13T13:58:10+05:30 [INFO] ———————————————————————— C:MVN> The above example illustrates the following key concepts − Plugins are specified in pom.xml using plugins element. Each plugin can have multiple goals. You can define phase from where plugin should starts its processing using its phase element. We”ve used clean phase. You can configure tasks to be executed by binding them to goals of plugin. We”ve bound echo task with run goal of maven-antrun-plugin. Maven will then download the plugin if not available in local repository and start its processing. Print Page Previous Next Advertisements ”;
Maven – Snapshots
Maven – Snapshots ”; Previous Next A large software application generally consists of multiple modules and it is common scenario where multiple teams are working on different modules of same application. For example, consider a team is working on the front end of the application as app-ui project (app-ui.jar:1.0) and they are using data-service project (data-service.jar:1.0). Now it may happen that team working on data-service is undergoing bug fixing or enhancements at rapid pace and they are releasing the library to remote repository almost every other day. Now if data-service team uploads a new version every other day, then following problems will arise − data-service team should tell app-ui team every time when they have released an updated code. app-ui team required to update their pom.xml regularly to get the updated version. To handle such kind of situation, SNAPSHOT concept comes into play. What is SNAPSHOT? SNAPSHOT is a special version that indicates a current development copy. Unlike regular versions, Maven checks for a new SNAPSHOT version in a remote repository for every build. Now data-service team will release SNAPSHOT of its updated code every time to repository, say data-service: 1.0-SNAPSHOT, replacing an older SNAPSHOT jar. Snapshot vs Version In case of Version, if Maven once downloaded the mentioned version, say data-service:1.0, it will never try to download a newer 1.0 available in repository. To download the updated code, data-service version is be upgraded to 1.1. In case of SNAPSHOT, Maven will automatically fetch the latest SNAPSHOT (data-service:1.0-SNAPSHOT) every time app-ui team build their project. app-ui pom.xml app-ui project is using 1.0-SNAPSHOT of data-service. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>app-ui</groupId> <artifactId>app-ui</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>health</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>data-service</groupId> <artifactId>data-service</artifactId> <version>1.0-SNAPSHOT</version> <scope>test</scope> </dependency> </dependencies> </project> data-service pom.xml data-service project is releasing 1.0-SNAPSHOT for every minor change. <project xmlns = “http://maven.apache.org/POM/4.0.0” xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>data-service</groupId> <artifactId>data-service</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>health</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project> Although, in case of SNAPSHOT, Maven automatically fetches the latest SNAPSHOT on daily basis, you can force maven to download latest snapshot build using -U switch to any maven command. mvn clean package -U Let”s open the command console, go to the C: > MVN > app-ui directory and execute the following mvn command. C:MVNapp-ui>mvn clean package -U Maven will start building the project after downloading the latest SNAPSHOT of data-service. [INFO] Scanning for projects… [INFO]——————————————– [INFO] Building consumerBanking [INFO] task-segment: [clean, package] [INFO]——————————————– [INFO] Downloading data-service:1.0-SNAPSHOT [INFO] 290K downloaded. [INFO] [clean:clean {execution: default-clean}] [INFO] Deleting directory C:MVNapp-uitarget [INFO] [resources:resources {execution: default-resources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:MVNapp-uisrcmainresources [INFO] [compiler:compile {execution:default-compile}] [INFO] Compiling 1 source file to C:MVNapp-uitargetclasses [INFO] [resources:testResources {execution: default-testResources}] [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory C:MVNapp-uisrctestresources [INFO] [compiler:testCompile {execution: default-testCompile}] [INFO] Compiling 1 source file to C:MVNapp-uitargettest-classes [INFO] [surefire:test {execution: default-test}] [INFO] Surefire report directory: C:MVNapp-uitarget surefire-reports ————————————————– T E S T S ————————————————– Running com.companyname.bank.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.027 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [jar:jar {execution: default-jar}] [INFO] Building jar: C:MVNapp-uitarget app-ui-1.0-SNAPSHOT.jar [INFO]——————————————————– [INFO] BUILD SUCCESSFUL [INFO]——————————————————– [INFO] Total time: 2 seconds [INFO] Finished at: 2015-09-27T12:30:02+05:30 [INFO] Final Memory: 16M/89M [INFO]———————————————————————— Print Page Previous Next Advertisements ”;
Maven – Useful Resources
Maven – Useful Resources ”; Previous Next The following resources contain additional information on Maven. Please use them to get more in-depth knowledge on this topic. Useful Video Courses DevOps CI/CD Pipeline With Jenkins Ansible Docker and Kubernetes Best Seller 42 Lectures 5 hours AR Shankar More Detail Learn Maven and Ant the easy way! Most Popular 35 Lectures 4 hours Karthikeya T More Detail JSP and Servlets – The Complete Course 43 Lectures 5.5 hours TELCOMA Global More Detail Learn Maven 14 Lectures 1.5 hours Quaatso Learning More Detail Maven Online Training 14 Lectures 5.5 hours Uplatz More Detail Create a Spring Boot Restful web app with Java 11 and Maven 11 Lectures 32 mins Dimitrios Malonas More Detail Print Page Previous Next Advertisements ”;