After a successful compile, you can run the class file on any computer that has a
This guide walks you through using Maven to build a simple Java project. Show
What you’ll buildYou’ll create an application that provides the time of day and then build it with Maven. What you’ll need
How to complete this guideLike most Spring Getting Started guides, you can start from scratch and complete each step or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code. To skip the basics, do the following:
When you finish, you can check your results against the code in Set up the projectFirst you’ll need to setup a Java project for Maven to build. To keep the focus on Maven, make the project as simple as possible for now. Create this structure in a project folder of your choosing. Create the directory structureIn a project directory of your choosing, create the following subdirectory structure; for example, with └── src └── main └── java └── hello Within the
Now that you have a project that is ready to be built with Maven, the next step is to install Maven. Maven is downloadable as a zip file at https://maven.apache.org/download.cgi. Only the binaries are required, so look for the link to apache-maven-{version}-bin.zip or apache-maven-{version}-bin.tar.gz. Once you have downloaded the zip file, unzip it to your computer. Then add the bin folder to your path. To test the Maven installation, run If all goes well, you should be presented with some information about the Maven installation. It will look similar to (although perhaps slightly different from) the following: Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00) Maven home: /home/dsyer/Programs/apache-maven Java version: 1.8.0_152, vendor: Azul Systems, Inc. Java home: /home/dsyer/.sdkman/candidates/java/8u152-zulu/jre Default locale: en_GB, platform encoding: UTF-8 OS name: "linux", version: "4.15.0-36-generic", arch: "amd64", family: "unix" Congratulations! You now have Maven installed. INFO: You might like to consider using the Maven wrapper to insulate your developers against having the correct version of Maven, or having to install it at all. Projects downloaded from
Spring Initializr have the wrapper included. It shows up as a script Define a simple Maven buildNow that Maven is installed, you need to create a Maven project definition. Maven projects are defined with an XML file named pom.xml. Among other things, this file gives the project’s name, version, and dependencies that it has on external libraries. Create a file named pom.xml at the root of the project (i.e. put it next to the
With the exception of the optional
At this point you have a minimal, yet capable Maven project defined. Build Java codeMaven is now ready to build the project. You can execute several build lifecycle goals with Maven now, including goals to compile the project’s code, create a library package (such as a JAR file), and install the library in the local Maven dependency repository. To try out the build, issue the following at the command line: This will run Maven, telling it to execute the compile goal. When it’s finished, you should find the compiled .class files in the target/classes directory. Since it’s unlikely that you’ll want to distribute or work with .class files directly, you’ll probably want to run the package goal instead: The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within
the target directory. The name of the JAR file will be based on the project’s To execute the JAR file run: java -jar target/gs-maven-0.1.0.jar
Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies. If you’d like to install your project’s JAR file to that local repository, then you should invoke the The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency. Speaking of dependencies, now it’s time to declare dependencies in the Maven build. Declare DependenciesThe simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and complex functionality. For example, suppose that in addition to saying "Hello World!", you want the application to print the current date and time. While you could use the date and time facilities in the native Java libraries, you can make things more interesting by using the Joda Time libraries. First, change HelloWorld.java to look like this:
Here If you were to run
This block of XML declares a list of dependencies for the project. Specifically, it declares a single dependency for the Joda Time library. Within the
By default, all dependencies are scoped as
Now if you run Write a TestFirst add JUnit as a dependency to your pom.xml, in the test scope:
Then create a test case like this:
Maven uses a plugin called "surefire" to run unit tests. The default configuration of this plugin compiles and runs all classes in or just use Here’s the completed
SummaryCongratulations! You’ve created a simple yet effective Maven project definition for building Java projects. Which programming feature allows you to use a method name to encapsulate a series of statements?Java Chapter 3. What type of high level programming language is translated into machine code once and then executed many times?Answer: Compilers and interpreters have similar functions: They take a program written in some programming language and translate it into machine language. A compiler does the translation all at once. It produces a complete machine language program that can then be executed.
What type of variable represents a location in the computer's memory that Cannot change once it has been assigned a value?A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant.
Is always the same as the name of the class whose objects it constructs?Answer: A constructor is a special kind of subroutine in a class. It has the same name as the name of the class, and it has no return type, not even void. A constructor is called with the new operator in order to create a new object.
|