0% found this document useful (0 votes)
207 views

Nataraj Sir (Maven)

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
207 views

Nataraj Sir (Maven)

Uploaded by

test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 42
ii NARESHO echmolo vires AN p ache Course Material By Mr. Nataraj Naresh i Technologies, Opp. Satyam Theatre, Ameerpet, Hyd. Office: 040- 23746666 Mobile: 9000994007, www.nareshit.com HighLights Introduction to Apache Maven ‘Apache Ant Versus Apache Maven Key Features of Maven Downloading and Installing Maven ‘Maven Repository Maven Archetype ‘Standalone Maven Projects ‘Web Apps through Maven Maven through Eclipse Maven Inheritance Maven MultiModule Project Life Cycle of Maven Maven Interview Questions and Answers | Work hard for what you want because it won't come have to be strong and courageous and know that | your mind to, If somebody | puts you down or criticizes } you, just keep on believing in yourself and turn it into something positive. Naresh /Technologies MAVEN By Mr. Nataral & Tear #Overview > In project development, execution and deployment there will be multiple complicated, repetitive operations, These operations are called build operations. Keeping our project ready for execution /release is called building project. Doing various activities. (of build process manually is complex and error prone process, To overcome this problem take the support of batch file and automate the process. ‘The limitations with .bat file is we cannot write conditional statements to perfarm build activities. To overcome this problem we can go for Ant build tool where we can automate build activities in conditional environment by using build.xmi file declaratively.. ‘Ant is just build Tool but it cannot be used for project management activities lke downloading jar files, maintaining repositories, providing standard directory structures and ete. To solve these problems of ANT we can use Maven tool which is not only build tool and itis also a Project management tool. #introduction to Apache Maven ‘Apache Maven is an advanced build tool to support the developer at the whole process of a software Project. Maven acts as both a dependency management tool t can be used to retrieve jars from @ central repository ot from a repository you set up - and as a declarative build tool. Maven allows the developer to automate the process of the creation ofthe initial folder structure for the Java application, performing the Compitation, testing, packaging and deployment of the final product. It is implemented in Java which ‘makes it platform independent. ‘Maven relies on conventions about how project directories are laid out in order to achieve its “deciarativeness.” For example, it has a convention for where to put your main code, where to put your web.xml, your unit tests, and so on, but also gives the ability to change them if you need to. The real out-of-box strength of Maven is its dependency management. You only have to declare the dependencies and Maven will download them, setup the classpath for you, and even deploy the dependencies with your application, if required. vs Maven Apache Ant versus Apache Maven ‘Ant doesn’t have formal conventions. You have to tell Ant exactly where to find the source, where to put the outputs, etc. Ant is procedural. You have to tell Ant exactly what to do; tell it to compile, copy, then compress, ete, ‘Ant doesn't have a lifecycle. Maven uses conventions. It knows where your source code is automaticaly, as long as you follow these conventions. You don’t need to tell Maven where itis. Maven is declarative; all you have to do is create a pom.xml file and put your source in the default directory. Maven will take care of the rest. ‘Maven has a lifecycle. You simply call mun install and a series of sequence steps are executed. ‘Maven has intelligence about common project tasks. To run tests, simple execute mvn test, a long as the files are in the default location. In Ant, you would first have to JUnit JAR file is, then create a classpath that Includes the JUnit JAR, then tell Ant where it should look for test source code, write a goal that compiles the test source and then finally execute the unit tests with 1Unit. ‘A Practical Approach To Learn Maven Naresh i Technologies MAVEN By Mr. Nataraj & Team #Key features of Maven > Maven tries to avoid as much configuration as possible, by choosing real world default values and supplying project templates (archetypes). > Can download jar files dynamically > Can maintain multiple repositories having jar files, plugins and etc.. > Provides standard project directory structures > Gives Maven inheritance to share jar files and plugin among the multiple projects ¥ Allows to develop multiple module projects > Can generate at, war, ear and ete... packaging based components or Application > Can run unit tests and can generate unittest reports > Can generate project documentation > Can clean and install the projects in the local servers or remote servers #Downloading and Installing Maven “The Maven project is hosted by the Apache Link Software Foundation, where it was formerly part ofthe Jakarta Project.in ase youwantto Nery tergrarehve apache-maven-3.8-in sargt this you just need to download the Maven’s Zip Source tar.gz archive apache-maven-3.8.9-stc 23° SE. choice, and configure the Windows Source ap archive apathe-maven-2.9.9-st¢ 26 environment variables. > To download the maven as a zip file, vist oficial website: http://maven.apache.org/download.cgl and click the above given linkapache-maven-3.3.-bin.2ip to begin the download and save It to any preferred folder of your choice. ‘System Properties Scere | Hass) Rivne’ | pen Pk Environment Variables ser varie for Chand Mand Before configuring maven make sure that $OK is installed in your system and set JAVA_HOME variable to java installation folder. Variable: JAVA_HOME Value: C:\ProgramFiles\Java\jdk1.8.0_73 (make sure there is no semi-colon at the end) ’A Practical Approach To Learn Maven Naresh i Technologies MAVEN By Mr. Nataraj & Team Now | suppose you have downloaded and unzipped maven software. I have extracted the maven zip file in the D:\maven folder. Type File foider 9 boot Filefoider Ji cont AOAN201S 1h38.Filefoider fe 0-11-2015 Ted. Filefelder hucense 2015 Het, File Notice DAT.20I5 Tt. File README 0-11-2015 1 Tet Docu > Add maven bin folder to PATH Variable Variable:PATH Value:\bin;; In my caseD:\maven\apache-maven-3.3.9-bin\apache-maven-2: Varsble name: Variable vale: > To verify if maven has been configured successfully, invoke myn ~version(or mun -v or version) in the command prompt + D:\maven\apache-maven-3.3.9-bin\apache-maven-3.3.9 Bava version: 1.8.0-73, vendor: Oracle Corporation Bava hone: c;\Progran Files\sava\jdk1.8.0./a\jre fault locale: en_IN, platform, encoding: Cpl252, S nane: “windows 8", version: "6.2", arch: "x86", family: you get the message as shown above, it means you have successfully installed and configured Apache Maven in your windows machine. pemembet! In maven, all configurations will be done in pom.xml file. ‘A Practical Approach To Learn Maven, Naresh i Technologies MAVEN By Mr. Nataraj & Team #introduction to Maven Repository According to Maven Official Documentation, “A repository in Maven is used to hold build artifacts cand dependencies of varying types.”A maven repository is a place i. directory where all the project, Jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easiy-There are three types of repository in maven. They are 1. Local 2. Central 3. Remote Local Repository: It is user specific repository, generally it wil be collected from TL/PL who creates Maven project directory structure. Contains jars, plugins, current project related packings and etc.. Default location: e:lusers \|.m2 ‘Will be created automatically for any maven command apart from (mvn -version) ‘CAUsers\Chandra Manima\reposvory Name a Type ent mm Filefo sopalliance 2016 1138.~. Flefolder am HE TH? Flefolder aepect IO-2016 1038.» Flefolder 4 0-08-2016 Filefolder > Location can be changed through \conf\settings.xm! file using d:\ \maven . Central Repository: ‘Ayalalble in internet, managed by Apache Maven Community. When Maven does not find any dependency in local repository, it starts searching in central repository. URL for central repository: http://repo.maven.apache.org/maven2 Generally maintains free jars and plugins of open source technologies Remote Repository: Either company specific or Vendor specific repositories Example UHG repository, Jboss repository Willbe used only when tag cfg.. in pom.xml ‘A Practical Approach To Learn Maven Naresh / Technologies MAVEN By Mc. Nataraj & Team [| We do all configurations in maven through pom.xml. iis the heart for maven tool. This pom.xml contains. project details, packing details (jar/war/ear and etc.), plugin details, remote repository detalls and dependency(jar) details and etc. 12> Maven first searches in Local repository 1.5>1f available > gives jar file to app ‘A Practical Approach To Learn Maven Naresh / Technologies MAVEN By Mr. Nataraj & Team "3? If not available in central repository, it will search in remote repository by checking tag in pom.xm! 4>If not available->error will be raised 4,5->1f available the jar will be downloaded to local repository and will be given to maven app. Every jar that is downloaded to local repository from central repository or vendor's specific remote repository. It is recommended to push to company specific cloud repository or company specific remote repository. #lViaven Archetypé It is @ maven project templating toolkit that helps users to create different types of maven project having maven supplied facilities and user supplied inputs. Nearly 1500+ archetypes are supplied by Maven to create different types of project. The popular archetypes are Archetype Artifactids [ Description ‘mavervarchetype-quiekstart | An archetype to generate a sample Maven project. maver-archetype-webapp ‘An archetype to generate a sample Maven Webapp ‘maven-archetype-j2ee-simple > Every project directory structure gives pom.xml with minimum details. We can add more tags in that as needed. > Every Maven Project, dependency jar, plugin and etc are identified with 3 minimum details. groupld> Generally company name atifactid->Project name/jar name/plugin name version->syn:-SNAPSHOT/RELEASE FINAL/BETA #Generating project through Maven We can create maven projects in any folder of your choice in any drive, Here | am creating it in d:\maven folder. To create a new project based on an Archetype, we need to call mn archetype:generate goal, lke the following: ‘#€xample1 (Generating projectin Interactive mode) ‘A Practical Approach To Learn Maven. Naresh / Technologies MAVEN By Mr. Nataraj & Team [Open the Command Prompt and change directory where you want to create your project and call mvn archetypesgenerategoal to begin the process. © ext. Ifthis is the fst me you execute tis goo this may tke some time. Tiss becouse Maven fist remem” ioads all required plug-ins and artifacts for the project generation fram the Maven centrol repository. D:\maven>mvn archetype-generate : ‘As soon as you hit enter button, it will start loading all the available artifacts and begins interaction with you like: Choose a number or apply filter (format: [groupld:Jartifactid, case sensitive contains): 902: Here you have to either enter the archetype number for which you are going to create your project or apply the fiter (nothing but fering archetypes from the lst loaded). First ofall, we are going to see how | to create the standalone project. So, type maven-archetype-quickstartand hit enter. Now, simply choose the archetype (hit enter key) and supply the properties configuration and confirm it as shown below. jighoose, a punber or apply filter (format: [aroubtd:Jartifactzd, ‘case sensitive co Intains)? 9027 naven-arel choose archetype i Fenote > org.apache.maven archetypes :maven-archetype-quickstart (An archetyp @ which contains a sample Maven project) Fercese a'nunter or apply ritter (Format: rorowra:iarcitactzd, casa sansiziva co f {choose org, apache maven archetypes smaven-archetype-quickstart version: TT: Lo-aipha-i 12: Lo-atpha-2 To-alpha-3 To-atpha-d Pio) stype-quickstart a nunber: 6: Value for property ‘grouprd": ; naresh Value for property ‘artifactid’: ; MathProji. Value for property ‘version’: 1.0-SNAPSHOT: value for property "package": faresh: : com.at Confirm properties configuration: grouprd: naresh, artifactid: MathProj1 Version: 1.0-SNAPSHOT fpackage: con.nt (beans 2 ‘A Practical Approach To Learn Maven, Naresh i Technologies MAVEN By Mr. Nataraj & Team “if you get BUILD SUCCESS (as shown In the diagram below), that means you have successfully wrested your first standalone maven project. fpinro] - JEINFO] Using fo\lowing parameters for creating project from Old (1.x) Archetype: [rravey-archerspe-autcketaresa-2 ae H[IWro) earamecer: basedir, ¥: ‘iinro) Paraneter: package, valu [FIMO] parameter: Groupie, valu INFO] Parameter: artifactid, Value: MathProj1 [INFO] Parameter: packageNamé, Value: com.nt [aMro} varaneter:: Version, Values, 1-0-SNABSHOT [INEO} project created fru 01d (1.x) Archetype in dir: D:\naven\Mathrrojs [INFO] Borie Success INFO} [INFO 32 min [INFO] Fini 2016-11-30714:47:02+05:30 eto M ‘TNE. After generating the project, the project directory structure looks like the following... d:\maven, [-—>MathProj1, |->Appjava | package com nt || public class App { public int addtint x,int yl return xy; , public static void main Stringl] args) System.out-printin| "Hello World!” J; ‘App app=new App int result= app.add(10,20); System.out printin("Result"+ result); System.out.printin|"End ofthe App"); Now start running the following maven goals... ‘Practical Approach To Learn Maven Naresh Technologies MAVEN By Mr. Natoraj & Team ‘\MathProji>mvn compile > Compiles the App.java (all source files) D:\..\MathProji>mvn package > Generates jar files in target folder having -ver.jar file D:\....\MathProji>mvn clean > Cleans the project .. deletes target folder (MathProji>mvn clean package > Cleans the project and also creates jar file with latest code Torrunjar file App manually MathProjt>java -cp target/MathProj1-1.0-SNAPSHOT.jar com.nt.App #Maven Unit Testing and Unit Test Report With the help of we can run the test cases can generate unit test reports in HTML. Maven does so by running the unit tests and recording the results of the unit tests. Maven then generates an HTML report from the unit test results, We can then read the report to see if any of the unit test have failed during the build, Open the AppTestjava java file(D:\maven\MathProji\se\test\java\com\nt) of MathProji. project and do the following changes in testAdd() method AnpTest,java @ part £Y default Maven’s pom.amigives tag to download junit jar. So, we ean write ner test cases in Proj\src\test\java\.....\AppTest.java) ublic void testAdd() { int x10; int actual=new App().add(10,20); Assert.assertEquals( "Result" expected,actual); Now let us run the above test cases and generate the test report. The Maven unit test reports are generated by the Maven Surefire plugin. Therefore such unit test report is also sometimes referred to as surefire report D2\..u\MathProji>mvn test > Torun these test cases D:\.....\MathProji>mvn surefire-report:report > Torun test cases and to get test report 'A Practical Approach To Learn Maven Naresh Technologies MAVEN By Mr. Nataraj & Team ‘D.\snue\MathProjl>mvn surefire-report:report-only > Toget test report directly (Note: We can get test report in target/site/surefire-report.html form) Da\...c\MathProjl>mvn install > Tokeep generated jar file {Application jar) in Local Repository The above jar file can be placed in local repository in other maven projects by specifying , , tags in tag. .\MathProjt>mvn site > To get documentation about total project. (generates project document in target/site folder as Index.htrn! file) ‘While writing tag to download the jar file, we can specify the scope by using tag. ‘The possible scopes are compile, runtime, provided, system, test, import etc {pom.xml jependency> supld>junit artitactid>junit rsion>3.8.1 scopertest ldependency> ‘Maven is having three life cycle and each life cycle is having certain phases. By specifying these lifecycle phases, we can configure plugins in pom.xml so that plugin will perform the activity at the specified phase. ‘The Maven Life cycles are a. clean(3 phases) b. default(23 phases) site (4 phases) pet! F YoU ask maven to pesfarm 17% phase (package) of default ie cycle, it also performs the Remet remaining 16 phases in a sequence ike compile, test-compile, test and etc. #Creating standalone maven project in non-interactive mode Syntax: ‘mvn archetype:generate ‘A Practical Approach To Learn Maven t Naresh / Technologies MAVEN By Mr. Nataraj & Team “Dgroupld=groupid Dartifoctidzartifactid -Darchetypeartifactid=maven-archetype-quickstart -DinteractiveMode=booleanValue Example: GenerateSqrRoot Here we will create a simple standalone project to find the square root of a given number. | am creating all ‘my maven projects in D:\maven folder. ‘mun archetype:generate -Dgroupidecom.nt -Dortifactid=GenerateSqrRoot -DarchetypeArtfactid=maven-archetype-quickstart -DinteractivetModesfalse Here, we are instructing the maven to create the project directly without interactive mode. Run the above command in command prompt specifying the path where you want to generate the project directory structure. (Caution:while executing the above command make sure it is in the single line as shown in the picture below.) :\naven>nvn archetype:generate -Dgrouprd-com.nt -Dartifactid=GeneratesqrRoot -D rchetypeart ifactid=naven-archetype-quickstart -Dinteractivelode=false Sipomami ‘As we can see in the above picture, after the successfull execution of the maven command, the directory structure is created with “App java” java file. Now let’s customize and write our own logic to genefate square root. Rename that file with “SqrRoot java” and write our own log ce maven + GensrateSqrRoct + sc > main > java > com > nt Neme Dote meelties GA sqootjave In SqrRoot.java package com.nt + Gonerate SquareRoot ofa given number ‘A Practical Approach To Learn Maven. Naresh / Technologies MAVEN By Mr. Nataraj & Team import java.util Scanner; import java tang. Math; public elass SqFRoot{ public state void main(String args{D{ Scanner s= new Scanner(System in}; System out printIn("Enter number te Gnd its square roa: ") intn=snextlnts System. out printin’Math sea); Wain Welass Now, we will compile and execute our standalone project with the help of maven. For this we need a plugin “exec-maven-phugin”. What actually is this plugin and what it does??? Official documentation says (http://wwes mojohaus.org/exec-maven-plugin/) Exec Maven Plugin provides 2 goals to help execute system and Java programs. + execexec execute programs and Java programs In a separate process. + exec;java execute Java programs in the same VM. So, we need to provide this information in pom.xml of the our GenerateSqrRoot project. ‘Add the following cfgs in the pom.xml of our project after or before tag. In pom.xml build plugins» org.codehausmojos/grounld> -arbfactid>exec-maver-pugine/anfactc> 1.8 Ge/version> com nt SarRoote/mainclass> Execute the project with following command Invoking “myn exec:java” on the command line will invoke the plugin which is configured to execute the class “com.nt-SqrRoot”. So, to trigger the plugin from the command fire, just run: D:\maven\GenerateSqrRoot>mvn compile "A Practical Approach To Learn Maven Naresh i Technologies MAVEN By Mr. Nataraj & Team ‘Once code is compiled, the following command runs your class D:\maven\GenerateSqrRoot>mvn package :\maven\GenerateSqrRoot>mvn exec:java (Note: while maven starts execution, supply your desired integer value to get the square root) 3 aaa ae Hiro| Toval vine? 01225 ain [Liste] Pintehed" as 2038-42" sora7:18:0800s:30 Naa. Now let’s run the same project with little bit of twist. We can run the same project by specifying the class name in the command itself. For this we have to remove the related tag in the pom.xml inside tag that we just placed, So that ‘our pom.xml now looks like this Ja pom.xml sould plugs ~aroupldborgcodehausmolo exec maven plugincfarttactié> version L.A.de/version> “
    Syntax for execution smn exec:java -Dexec.maincloss="com.nt.module MainClassName" Execution: D:\maven\GenerateSqrRoot>mun exec;java -Dexec.mainClass="com.nt.SqrRoot” \naven\GeneratesqrRoat>mun exec: java ~Dexec.nainclass="con.nt-SarRoot™ (Enro] Scanning for projects. FINE FINO: INFO] Building GeneratesqrRoot 1.0-SNAPSHOT FINE: INFO. [xNro] --~ exec-maven-plugin:1.4.0:java (defauTt-cli) @ GeneratesgrRoot ater nunber to find ies square root: Let’s explore more ways to run the same project. Now, if you want to execute the execijava goal as part of your standard build, you'll need to bind the goal to a particular phase of the default lifecycle. To do this, declare the phase to which you want to bind the goal in the execution element: ‘A Practical Approach To Learn Maven, 4, Naresh Technolsies maven by Mr. Natara & Team For is werneed to spechy cphose> iphase of your choke Me paclage, test cic) and goa tape | pom.xml. So, the changed pom.xml looks like the following. In pom.xml bul arousing colehous moje ouslt> cortectzerecmover pig fertoce> verion>14.0/resion> stomp erecsonchts onto skonuraon> plugs Execution: D:\maven\GenerateSqrRoot>mun package (Note: This executes com.nt.SqrRoot class during the package phase) SIC Restraca ce wcaerttts Example: SwapDemocmd This is just a proof of concept example. Here we are going to see how to specify command line sxguments through command line along with maven commands and how to specify arguments in the pom.xml through tag. Let's do it by creating a fresh maven project. ‘myn archetype:generate ’A Practical Approach To Learn Maven iS Naresh i Technologies MAVEN By Mr. Nataraj & Team ~ -bgroupld=comnt as -Dartifactld=SwapDemotmd -DarchetypeArtifactid=maven-archetype-quickstart -DinteractiveMode=false Fo:\mavensuvn archetype:generate -pgrouprd=com.nt -Dartifacttd=swapDenocnd -Darch [etypeartitactid-naven-archetype-quickstart -pinteract ivetode=false This command will generate a directory structure for SwapDemoCind project with “App java” as a default java class. Let's change it with “SwapDemojava”. aap EEO BRET] aa 6 Spouse ye» eS com > of item Name : Date mosied 4 apache-maven-33..bin 1 amet 3 GanerteSerRost Bswapdenejava 7-08-2016 O48. In SwapDemo java package com. a * swapDemo a class Swap{ int abs void acceptint x, int y}{ boy, , void swepValues(}{ aa; beob; ) void displ} System outprintin(*Value of a: "+2); System.out.printl ‘A Practical Approach To Learn Maven Ie Naresh / Technologies MAVEN By Mr. Nataraj & Team public static void main String argsI) //Check f two values are entered as em line args or not ifergstength!=2) System.outprintln("la enter two values"; else intxt=integer.parselnt(argst0)) Int x2=integer.parseint(argstt)) Uereate an object of Swap cass Swap sw= new Swapl); ‘sw accept(xt, x2); Systern.out.printin( "Before swapping’ System.out print sw.disp(); sw.swapValues(); ‘System.out.printin( “After swapping System.out.printin(" sw.disp(); ) Mielass ‘Now, do some changes in pom.xml of SwapDemoCmd project. In pom.xml exec-maven-plugins/artfactla> 1.4,06/version> -org.apache.maven plugins moven-archetype-plugin -version>2.4 <(olugin> ‘A Practical Approach To Learn Maven Naresh/ Technologies MAVEN By Mr. Nataraj & Tear Execution: Since we have to pass command line arguments to execute this project, let's see the syntax to do so. Syntax ‘mun exec:java -Dexec.mainClass="com.nt.module.MainClassName" -Dexec.args="arg0 arg1 arg2” Here, | will pass 8 and 4 as arguments. So the command looks like this. D:\maven\swapDemocmd>mvn compile \maven|SwapDemoCmd>mun package _D:\maven|swapDemocmdsmun exer:Java -Dexec.mainClass="com.nt.SwopDemo” -Dexec.args="8 4" java -Dexec.mainclass="com.nt .Swappeno” -Dexec.arg : \naven\swapDemecndsmvn exe =e 4 INFO INFO| NFO] Guilding Swappenoced 1. INFO| ial Scanning for projects... Finished at: 2016-11-30737: FEMS) Finat Menor: oh/i6R {INFO =="=- Wouldn't it be great if we can specify arguments in the pom.xml itself and run the project ina maven phase? For example, we can run the SwapDemo.main() method as part of the package phase. To specify the arguments in pom.xml, we write under tag, So, mention the following configurations in pom.xml of SwapDemoCmd project. In pom.xm| build -org.codehaus.mojo exee-maven-plugine/artifactld> 1.4.0%/version> packege ‘A Practical Appraach To Learn Maven Naresh / Technologies MAVEN By Mr. Nataraj & Team “esneleea -com.nt SwapDemos/mainClass> 3 -S < Execution: To run the exec plugin with above configuration, simply ran the corresponding phase. D:\maven'SvapDemoCmd>mvn package #Converting Maven standalone project into eclipse project D:\maven\SwapBemoCmd>mvn eclipse:eclipse Open SwapDemoCmd from Eclipse using File menu->import->General>Existing Project into Workspace >Browse the Root Directory (ie. SwapDemoCmd) of the project. #Generating Maven web project by using maven-archetype-webapp ‘mvn archetype:generate -Dgroupid=com.nt.serviet ~Dartifactid=WishApp -DarchetypeArtifactid=maven-archetype-webapp -Dpackage=com.nt.serviet -DinteractiveMode=false mnaven>nvn_archetype:generate -Dgrouprd=com.nt.servlet ~Dartifactid=WishAjp —r archetypeartifactId=maven-archetype-webapp ~Dpackage=com.nt.serviet -Dinterdetiy eMode=ta Hit enter key and you will get the following director structure ‘A Practical Agproach To Learn Maven Naresh i Technologies MAVEN By Mr. Nataraj & Team In com/nt/serviet folder->WishServiet.java, copy the following code In WishServietjava |--serviet package com.nt.serviet; |--WishServletjava Amport java.io-Toexception; import Java.io.Printuriters import Java util calendar; import Jave.util Dates Amport javax.serviet, ServietException; import Javax.serviet .nttp.HttpServiet} import Javax.serviet.netp.HttpservietRequest; import Javax.serviet.http.HttpServietResponse; publicclassiishServietextends HttpServiet { publicvoid doGet (HttpServietRequest req, HttpServletResponse res) throws IOException, Sorvletéxception { W/ General setting res. setContentType( “text /html") ; Printhiriter pw = res.gethriter(); Calendar calendar = Calendar.getInstance() ; int hr © calendar.get(Calendar.HOUR_OF_DAY); Date d = new Date(); pa.println(""); iF (hr 2) pw.print1n(*

    G000 MORNING

    elseif (hr <= 16) w.printin("

    6000 AFTERNOON

    ")3 elseif (hr <= 20) pw.printn(*

    G000 EVENING

    ")5 else { pu.printin("

    c000 NIGHT

    "); y J/elose the stream pw.close(); Wi doGet(rea,res) ferride L 'A Practical Approach To Learn Maven. Zo Naresh i Technoogies MAVEN By Mr. Nataraj & Teem Publicvold aoPost(Httpserviethequest req, HrtpservietResponse res: ServletException, IOException { // ToDO Auto-generated method stub ocer(req,res); W/doPost(-)~) W/ class Inindexiso cht ‘ eRDHTML-SERVLET CONMUNTCATTONC/ I> ca href= "Hurl ">GenerateWishMessagec/a> In WishApp/porn.xmi(Add the following under tag) javax.servlet cartifactid>javax.serviet-api eversion>3.1.0¢/version> providedc/scope> In.web.xmnl ob -2pp> wish wish /wurl « index. jsp « Execution D:\maven\WishApp>mvn package > When you get BUILD SUCCESS while executing the above goal, one war file will be created in target folder of WishApp application. (:|maven\WishApp|target\Wishapp-1.0-SNAPSHOT.war) Now, start the tomcat server and deploy the generated war file (copy and paste WishApp-1.0- SNAPSHOT. warin webapps folder} > httpi//localhost:4040/WishApp-1.0-SNAPSHOT/ #Working with Tomcat7 Maven Plugin > We can even work with embedded tomcat server and deploy the generatedwar file. For this we have to use Tomcat? Maven Pluginand provide its configuration in pom.xml. Place the following piece of code in WishApp/pom.xml after or before tag. ———— ‘A Practical Approach To Learn Maven. 2r Naresh / Technologies MAVEN By Mr. Nataraj & Team WishAppe/finalName> org.apache-tomcat. maven tomcat7-maven-plugins/artifactld> 2.1 Instagram
  • Scribd - Download on the App Store
  • Scribd - Download on the App Store
Language: