Establish working environment - PawelBogdan/BecomeJavaHero GitHub Wiki
Java is modern high-level programming language. It supports many programming paradigms. It was created to create object oriented programming, and we will be focused on this approach.
Java source code is compiled to form called bytecode
, which is executed by Java Virtual Machine. There are many implementations of Java Virtual Machine. This fact let us write application once and run it everywhere (if only JVM is installed).
Java Virtual Machine is free software distributed in two packages:
-
JRE
- Java Runtime Environment. This software enable us only to run compiled to bytecode applications -
JDK
- Java Development Kit. This software is necessary to compile Java applications. It also let run compiled apps.JDK
contains more useful tools for Java developer.
We need to install JDK
. The newest Java release is 1.9. To check if we have Java installed and configured. Default localization of Java is directory: C:\Program Files\Java
. To check if we have everything configured properly execute command:
> java -v
> javac -v
If no error appears, you have everything configured.
Maven is very useful tool to build Java applications and to manage dependencies. We can establish new Maven project ourselves, but Maven has mechanism of creating projects from archetypes included.
Let's start with checking if me have Maven installed. Open application cmd.exe
and type
mvn -v
If no error appears, you have Maven installed properly. If you obtain error, follow instruction here.
If you have Maven installed. We can proceed to the next step. We can create proper Maven project using one command in command line:
mvn archetype:generate
-DgroupId=1
-DartifactId=2
-DarchetypeArtifactId=3
-DinteractiveMode=false
In place of 1
you should insert the packaging of your project. Usually the packaging is inverse of company's domain. For example, the company's domain is: acme.org.eu
, then packaging of its software usually looks: eu.org.acme
.
In place of 2
you should insert the name of project.
In place of 3
you should insert one of archetype's id. List of some of possibilities can be obtained here. In the beginning we will use archetype called maven-archetype-quickstart
. We can treat archetypes as templates for our projects.
If I execute command:
> mvn archetype:generate \\
-DgroupId=pl.edu.bogdan \\
-DartifactId=TestProject \\
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false
I obtain a new directory called TestProject
with a structure:
<full path>\TESTPROJECT
ββββsrc
ββββmain
β ββββjava
β ββββpl
β ββββedu
β ββββbogdan
ββββtest
ββββjava
ββββpl
ββββedu
ββββbogdan
The most important file is pom.xml
. Its content is:
<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>pl.edu.bogdan</groupId>
<artifactId>TestProject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>TestProject</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
As we can see there is tag dependencies
which contains information about libraries our project needs. Now, there is one dependency to JUnit
library - this library is used to create tests (we will learn about it later).
One of the biggest advantages of Maven is that it downloads correct libraries and uses it despite of platform where used. It means that our library is always built in the same way we defined it.
Maven should be run in the location of ```pom.xml`` file. According to documentation Maven builds our apps in following phases:
validate
compile
test
package
verify
install
deploy
Running Maven you can choose on which goal you want to stop. For example command mvn package
will validate, compile, test and package our app. There are some additional goals:
clean
site
We usually will run mvn install`` and sometimes we will use option
-DskipTests```.
For more information you can check here and here. I used those sites while creating this document.
- Create simple Maven project using command line
- Check difference between
package
andinstall
Git is one of the most popular version control system. One of the advantages of Git is that it can be used in distributed way, I mean there can be many servers which are synchronised with each other. We will not use many features of this tool. But I will create handouts for you and I will publish them on github.
We won't use git using command line. Every IDE has feature to use various number of version control systems. You need to know that git uses two kinds of repositories: local
and remote
. As usual you commit
your work, but commit is stored in local repository by default. You can send your commits to remote repository by push
command and obtain data from remote repository by pull
command. In fact you commit to specified branch, and you push and pull commits of specified branch. You can merge branches.
For more information go here and here.
Creating local repository
$ git init
This command creates empty repository.
If you have your local repository you can synchronise it with remote repository:
$ git add remote origin <url with .git in the end>
This adds remote repository called origin.
You can obtain the whole content of your remote repository:
$ git pull origin master
This command means get from origin repository all information about branch called master.
You can create your new branch:
$ git checkout -b <name of your newly created branch>
When you execute this command you will observe that information about branch appears in the terminal. You can change branch to other one executing command:
$ git checkout <name of existing branch>
Let us assume we are in newly created branch. We can change something in files. If we want to put information about our changes we need to execute commands:
$ git add <list of changed files>
$ git commit -m "<commit description>"
Of course you can replace git add <list of changed files>
by git add .
called from main directory of repository.
It is worth to notice that you can be asked to configure your git client:
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
You should do so, because each commit has author specified. You can observe this executing command:
$ git log
which displays history of the repository.
To send information about commits on specified branch to remote origin repository we need to execute:
$ git push origin <branch name>
Fortunately, git client asks about credentials to github, but you can configure your environment to use ssh keys. For more information see here.
You can also add your changes to master branch. To do so you need to change your actual branch:
$ git checkout master
and then call command
$ git merge <name of branch>
You can clone repository using one command:
$ git clone <address of repository>
This command initializes new local repository, sets remote origin to address provided in command, obtains master branch.
- Create github account (use your private mail account)
- Create repository on github page
- Create local empty repository
- Attach your github repository as remote origin for local one
- Do some changes on newly created branch and send them to remote origin
- Merge changes to master branch
- Checkout my repository:
https://github.com/PawelBogdan/BecomeJavaHero.git
As homework I recommend lecture of this short tutorial. We will back to SVN software in the future.
I am using Eclipse Oxygen:
Eclipse IDE for Java EE Developers
I will use this version whenever I want to show any screenshots and also during trainings.
- Create simple Java project (
File
->New
->Other
->Java Project
), add simple class, and execute project. - Create Maven project using Eclipse
- Checkout Project
Training001
from my repository:https://github.com/PawelBogdan/BecomeJavaHero.git
- Create your own branch using Eclipse IDE. Do some changes and push your branch to my repository (firstly I will need your logins to do add you as contributors)