For the sake of not repeating the tutorials that are already available online, and they are good tutorials if you will take time and read it. I'm going to list down the helpful links first before writing down the things I've done to create my Maven project in Mac and converting my java project to a "mavenized" one.
- http://www.mkyong.com/tutorials/maven-tutorials/
- http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
- Combine the resources and tutorial you'll find in these 2 links and you should be able to understand what Maven is and why create a project in Maven.
Pre-requisite: Installed Maven in your machine, how to know if you have maven? mvn -version
If it didn't show any maven version, then you have no maven installed.
I'm using IntelliJ Community Edition 14. I'm using this instead of eclipse because when my friend was having a problem with pom.xml, eclipse didn't return any error but just compiled the project without returning any error. On the other hand, IntelliJ returned the error about the pom.xml and my friend was able to proceed with the project. Since then, we've been using IntelliJ in creating our Selenium Webdriver scripts.
You can actually create a maven project straight from IntelliJ, but I won't go through that since that's pretty much documented in IntelliJ's website. So I'll be documenting the steps I did to create a maven project, import it in IntelliJ and then transfer all my java classes from Java project to a Maven project.
- Run your terminal / console
- Go to the directory where you want your maven project created
- Generate maven project: Type this to your terminal without quotes "mvn archetype:generate"
When you press enter, this command you type is not complete to create a maven project, so maven will have to ask you some things first before it can proceed.
It'll ask you a number or filter you want to apply, something like this...
$ Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 604:
If you know the number you want for the project, then type away. But if you don't know, you can simply copy the number it's showing. So in this case, the number 604. Based on my understanding, and what happened after I've created the project, the number that was shown was the archetypeID for a quickstart project in maven.
Then you'll be asked for the following details too...
Group ID: package where you want your class to run (example: org.company.bank)
Artifact ID: this is your project name (example: TestBank)
Snapshot 1.0: version of your maven, you can press enter and leave it blank or put the desired number
Package: you can leave it blank if you want your group id as your package already
Then to finish, enter "Y" to proceed, or "N" if you want to update/change something.
And all these steps can all be summarized to this one liner code...
$ mvn archetype:generate -DgroupId=org.company -DartifactId=TestBank -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- Run your IntelliJ and click on File > Import Project
- Choose or look for your maven project and click OK.
- Choose Maven and click next.
- If you have any specific project settings you need to adjust, you can do it now, or do it later. Click next.
- Then select your maven project and click next.
- Choose the SDK version the project will use
- Finally, the project name and click finish.
Now when you open your project, you might need to double check if your Maven in IntelliJ is pointing to the right location of maven. You can choose to override it and point it to your Maven's home. You can find where your maven is located when you do the mvn -version in ternminal. You can check with IntelliJ how to look for maven properties.
Now, next major thing you have to sort or fix is the pom.xml, why? Because maven needs the settings declared in pom.xml to build / run the whole project. Without proper pom.xml, your maven project won't run.
What's the quickest way to understand the pom.xml? Think of all your libraries or modules settings in your java project that you have to setup before compiling your project, those libraries have to be in your maven project too. But wait! You don't do it like in java project, because remember it's Maven project. So you have to use the pom and declare dependency to each libraries you have placed or used in your java project.
Quick example in my case...
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.modirum.generic.webdriver</groupId>
<artifactId>Selenium-Webdriver-3ds</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Selenium-Webdriver-3ds</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testing.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
As you can see here, you can get all these dependencies in http://mvnrepository.com/
You search for your library or jar, you click the project, click the latest version and then copy paste the dependency to your pom.xml
You'll see testng 6.9.4, log4j 1.2.17, and selenium-java 2.45.0
Then I have a surefire plugin to execute my testing.xml suite file. The plugins are not necessary if you don't need it yet, but like in my case, I've been using testing.xml in my java/testng project, so I need to add a plugin that will allow me to call testing.xml.
And every time you delete or add dependency, you'll notice that maven downloads the library immediately and stores it in your local maven repository.
You'll see your IDE loading whenever you add or remove a dependency. Initially the version of the dependency will be marked as red, that only means it's not yet available in your local repository, but once it's downloaded the red color of the font will become black, signifying that the download was successful.
And to test or run your maven project, you have to go to Run > Edit Configurations
and configure a maven command. For example, I want to run "mvn test" I'll have to create this setup...
- Type a run config name: maven test
- Working Directory: <my maven project>
- Command line: test
- Profiles: <blank>
- Click apply, then run.
Then one last step is importing your java tests files to maven.
In maven you have to follow certain file structures or directory layout, you should have seen it, because it's one of the link I mentioned above.
The src/main -> is basically where all your application and files related to application should go
while src/test -> is where our test web driver scripts should go
Why is this structured this way? As much as possible, maven wants to standardized the structure of the project. The archetype that was mentioned earlier are like templates for the kind of project you want your maven structure to pattern with. So, just setup the package you need under src/test and then copy and paste the java files to the right directory / package.
With IntelliJ, you don't have to one by one open your files and change the package name from your old java project to the new one, because it can do that for you. So all you have to do is copy, paste, wait for IDE to finish, and viola! You now have a "mavenized" project!
Typically when you do your first mvn test, you get some errors, but that is if you've missed some things like path to your csv or xls file. Or you have missing dependency in pom.xml and so on... but if all is good, it should run. So learn to read the error it's throwing, so you know what's the problem. And most likely the error you'll encounter has been encountered by other people so you'll find the answer when you google it. ;)
I hope this helps, and have fun with your project! :D