1. Summary
-
Maven
-
Exceptions
-
Classes with Self References
-
Classes with Mutual References
Work through the lab sections in the order that they appear. The lab is designed to provide extra questions for you to practise and as such you are not expected to finish the lab during our lab session. We recommend that you do eventually complete the lab on your own time. |
2. Maven
-
compile our code
-
compile our test code
-
run our test code
-
analyze our source code
-
generate reports about our source code
-
package our software
-
deploy/share our software
All code created as part of this class must use maven and the maven configuration covered in this lab |
2.1. Install Maven
2.1.1. Through your IDE
The latest versions of IntelliJ and Eclipse come with Maven as part of the IDE so you do not have to install anything if you are going to use Maven from within your IDE.
2.2. Maven with IntelliJ
IntelliJ has a specific project type for Maven projects.
-
Clone your lab3 repo in a location on your computer. We will use this location later when we setup our project.
-
Start IntelliJ and create a new project. see Lab1 for instructions.
-
In the
New Project
selection window, in the left hand pane, instead of selectingJava
selectMaven
. -
Once you select
Maven
in the left hand pane the right hand pane populates with archetypes. -
Go to the top of the right hand pane and check the box with the title
Crete from archetype
. -
Then click on the button Add Archetype…. We have created a custom archetype for CS5004 that we need you to import into IntelliJ. We will import the CS5004 archetype now and you will have it available for the rest of the semester. As soon as you click the button the
Add Archetype
window appears. Copy paste the following information in each box in this order-
GroupId :
edu.neu.ccs.cs5004
-
ArtifactId :
assignment
-
Version :
1.0
-
Repository (optional) :
https://raw.github.com/therapon/cs5004/mvn-repo
-
-
Click OK. This will add the class archetype into list of archetypes known to IntelliJ [2]
-
Find the newly added archetype in IntelliJ’s the list of archetypes. The name of the archetype should be
edu.neu.ccs.cs5004:assignment
. -
Select it with your mouse and click Next
-
In the next window we get to define the group and artifactId for your project will in the following information
-
GroupId :
edu.neu.ccs.cs5004
. This is the value is the same for all projects for this class. -
ArtifactId :
lab3
. This is the value that you can alter for each assignment and lab. So if you are creating your assingment 3 project you can write in this boxassignment3
. We are doing this for lab3 so we named itlab3
-
Leave Version intact.
-
-
Click Next
-
The next window will show all the information set up to this point. Nothing to do here so click Next
-
The next window is something you have seen before. Here you can give
-
A name to the IntelliJ project. This can be any name that you prefer.
-
Set the folder that IntelliJ will use to store all files in this IntelliJ project. This should point to the folder on your computer where you cloned your repo.
-
-
Click Next. IntelliJ will open the new project and will download all the configurations that we have set in place for CS5004. IntelliJ will open the file called
pom.xml
. While IntelliJ is downloading the CS5004 config the contents of this file will start getting updated. You will also see messages in IntelliJ’s console window. Do not click or type anything until you see similar lines to the ones below at the bottom of your IntelliJ’s console window.[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.174s [INFO] Finished at: Thu May 19 14:42:47 PDT 2016 [INFO] Final Memory: 10M/245M [INFO] ------------------------------------------------------------------------ [INFO] Maven execution finished
Once you see these lines in your IntelliJ’s console window, IntelliJ and Maven are setup and configured based on our class' settings.
-
Go to your Project Explorer window and expand the node with your IntelliJ’s project name.
-
Expand the folder named
src
. -
You should see two subfolders.
-
main
this is the folder that you must use to store all your Java sources. If you expand the folder namedmain
you will see a folder structure that maps to the groupId that we entered as part of the project creation steps. The folder structure follows the Java conventions for Java packages. You will be creating more nested packages under this location for your labs and assignments. -
test
this is the folder that you must use to store all your JUnit Test classes. If you expand the folder namedtest
you will see the same folder structure as the one inmain
. Maven allows us to separate our tests from our code while still keeping test classes in the same Java package as our Java classes.
-
-
We need to tell IntelliJ which folders contain Java source code.
-
We need to mark the folder named
java
undermain
as the Sources Root-
Navigate to the folder
java
under the foldermain
-
Select the folder named
java
. -
Right click on the folder named
java
and from the pop-up menu select Mark Directory As → Sources Root -
Notice that the color of the folder in your Package Explorer Window is now blue.
-
-
We need to mark the folder named
java
undertest
as the Test Sources Root-
Navigate to the folder
java
under the foldertest
-
Select the folder named
java
. -
Right click on the folder named
java
and from the pop-up menu select Mark Directory As → Test Sources Root -
Notice that the color of the folder in your Package Explorer Window is now green.
-
-
IntelliJ has a dedicated window that allows us to manage and use Maven. On IntelliJ’s main menu, oo to View → Tool Windows → Maven Projects. This should pop-up the dedicated Maven window located in the top right corner of your IntelliJ window.
-
NOTE when you minimize the Maven Window IntelliJ places a vertical tab in the right margin of the IntelliJ window with the tab title
Maven Projects
-
Use your mouse to click on the vertical tab named
Maven Projects
. TheMaven Projects
window should maximize.
-
-
The menu of the
Maven Projects
window has the same icon we use to run our tests; a green "play button". Click it. This starts the configured build process and will take some time to complete. The configured build process causes maven to-
Build your code
-
Build your tests
-
Run your tests
-
Run our reports
-
Generate HTML reports including
-
Javadocs
-
Code coverage reports
-
Checkstyle reports
-
Findbug reports
-
PMD reports
Pay attention to IntelliJ’s console window. You will see a lot of messages and information. The process should end with lines similar to the ones below
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.778s [INFO] Finished at: Thu May 19 14:55:56 PDT 2016 [INFO] Final Memory: 56M/776M [INFO] ------------------------------------------------------------------------ Process finished with exit code 0
This indicates that the build was Successful.
-
-
-
Go back to your Project Explorer window. You should be able to see a new folder named
target
. Maven places all build output inside this folder. [3] -
Expand the folder named
target
, and then expand the sub-folder namedsite
. -
Inside the folder named
site
you should see a file namedindex.html
. Use your mouse to right-click onindex.html
and select Open in Browser → Default to open this file in your web browser. [4] -
Your web browser should open and render the file
index.html
. The web page should have a menu in on the left hand side of the window. Locate the link titledProject Reports
and click on it. -
You should see the following links
-
JaCoCo Tests. This reports contains your tests' code coverage.
-
Source Xref. This reports contains all your source code as HTML files with links.
-
Checkstyle. This report contains violations of the Google Java Style guide found in your source.
-
FindbBugs. This report contains violations to other rules that we have setup for you.
-
JavaDocs. This is all your generated Javadocs.
-
-
There is one more report, the PMD report, that will appear once we add more code to our project.
3. Maven reports.
-
Create a package under your lab3 IntelliJ project with the name
edu.neu.ccs.cs5004.lab3.shapes
-
Bring in the source code for
Shapes
that we developed in Lecture 2 and place it under your new package. Do not bring in any Test code. -
Make sure you fix any errors that IntelliJ detects.
-
Run maven to build your code and generate all of its reports.
-
Open the
index.html
file to inspect all of your reports. -
Click on
Project Reports
3.1. JaCoCo Test
This report shows your test code coverage. Since we have no tests, your code coverage should be 0.
3.2. Source Xref
This report shows your code as HTML with links embedded so you can browse your code using the Web Browser.
3.3. Test Source Xref
Similar to Source Xref but for your JUnit code.
3.4. Checkstyle
This report shows any violations that you might have based on the Google Java Codestyle. The code should have some violations already.
-
Click to see the report.
-
You should see the following sections
-
Summary, contains the files and types of violations as a summary
-
Files, contains links to each source file that we have violations
-
Rules, contains the codestyle rules that are violated
-
Details, contains one sub-section for each file and lists the
-
Severity, Category, Rule, Message and a link to the line number in the source file that the violation occurred
-
-
3.5. FindBugs
This report shows any violations that have to do with, style, performance, security and possible bugs. Our Shapes
code should not generate any
FindBugs violations. In case that your code does generate FindBugs violations you should address those. Assignments will explicitly tell you which
FindBugs violations you should address and which FindBugs violations you can safely ignore.
3.6. PMD
This is another report that checks for style violations, design issues and other possible bugs. You may see some violations for our Shapes
project. It will depend on the issues you will have already fixed in your checkstyle report.
4. Exceptions
In our last lecture we talked about Exceptions in Java and we wrote one for Circle
.
5. Recursive Data Definitions
Recall our implementation of Lists from last lecture.
5.1. List of Posn
We would like to design a list whose elements are Posn
's and provide some operations on this list that are specific to Posn
's.
6. IntelliJ tips
6.1. Indentation
You can configure IntelliJ in order to use 2 spaces instead of 4 for each indentation level
Go to Preferences → Code Style → Java. Make sure the following items are set to these numbers
-
Tab size :
2
-
Indent :
2
-
Continuation Indent :
4
6.2. Adding curly braces to all if
blocks
Use one of the methods described in the IntelliJ documentation to configure your code inspections.
-
When the inspection configuration menu pops up locate the search box, the text box with an icon of a magnifying glass.
-
Type the following in the search box
Control flow statement without braces
. It should filter all inspections and leave you with the one namedControl flow statement without braces
. -
Select the checkbox at the right of the text
Control flow statement without braces
that appears below the search box. -
Click OK
-
Go back to your editor window. In the main IntelliJ menu select Analyze → Inspect Code. This will run all IntelliJ inspections and report issues.
-
The issues from your inspection will pop-up a window at the bottom of the IntelliJ window.
-
Expand
Code style Issues
-
Double click on the issue with the text
'if' without braces
. IntelliJ will open the line that violates the rule in your editor so that you can see it. -
Right click on
'if' without braces
and selectAdd Braces
-
Some times instead of
Add Braces
it will say something close toApply fix "Add Braces"
-