A makefile is a special file that lists a set of rules for compiling a project. These rules include targets, which can be an action make needs to take (eg. "clean" or "build") or the files/objects make will need to build (eg. .o files or an executable), and the commands that need to be run in order to build that target. When you call the program make, it runs through each of these targets in your Makefile and executes them.
target: dependencies
steps to build target with dependencies
CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
# $@ is the left side of the :# the $< is the first item in the dependencies list%.o: %.c $(DEPS)$(CC) -c -o $@$<$(CFLAGS)hellomake: hellomake.o hellofunc.o $(CC) -o hellomake hellomake.o hellofunc.o
clean:
rm -f *.o hellomake
Makefile -v4
CC=gcc
CFLAGS=-I.
DEPS = hellomake.h
OBJ = hellomake.o hellofunc.o
%.o: %.c $(DEPS)$(CC) -c -o $@$<$(CFLAGS)# $@ and $^ are the left and right sides of the :, respectively,hellomake: $(OBJ)$(CC) -o $@$^$(CFLAGS).PHONY: cleanclean:
rm -f *.o hellomake
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
validate: validate the project is correct and all necessary information is available
compile: compile the source code of the project
test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package: take the compiled code and package it in its distributable format, such as a JAR.
Maven Phases Continued
integration-test: process and deploy the package if necessary into an environment where integration tests can be run
verify: run any checks to verify the package is valid and meets quality criteria
install: install the package into the local repository, for use as a dependency in other projects locally
deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< cn.edu.nju:hello-world >-----------------------
[INFO] Building hello-world 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.1/junit-4.13.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.1/junit-4.13.1.pom (25 kB at 11 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.1/junit-4.13.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/4.13.1/junit-4.13.1.jar (383 kB at 14 kB/s)
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running helloworld.MathTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.498 s
[INFO] Finished at: 2020-11-15T15:50:36+08:00
[INFO] ------------------------------------------------------------------------
Maven Concepts
POM Files
Build Life Cycles, Phases and Goals
Dependencies and Repositories
Build Plugins
Build Profiles
Maven POM Files
A Maven POM file (Project Object Model) is an XML file that describe the resources of the project. This includes the directories where the source code, test source etc. is located in, what external dependencies (JAR files) your projects has etc.
The POM file describes what to build, but most often not how to build it. How to build it is up to the Maven build phases and goals. You can insert custom actions (goals) into the Maven build phase if you need to, though.
Running Maven is done by executing the mvn command from a command prompt. When executing the mvn command you pass the name of a build life cycle, phase or goal to it.
mvn install
This command executes the build phase called install (part of the default build life cycle), which builds the project and copies the packaged JAR file into the local Maven repository. Actually, this command executes all build phases before install in the build phase sequence, before executing the install build phase.
Build Life Cycles, Phases and Goals
Maven contains three major build life cycles:
clean
default
site
Inside each build life cycle there are build phases, and inside each build phase there are build goals.
Executing
You can execute either a build life cycle, build phase or build goal. When executing a build life cycle you execute all build phases (and thus build goals) inside that build life cycle.
When executing a build phase you execute all build goals within that build phase. Maven also executes all build phases earlier in the build life cycle of the desired build phase.
mvn clean
mvn clean install
Default Life Cycle
The default life cycle is the build life cycle which generates, compiles, packages etc. your source code.
You cannot execute the default build life cycle directly, as is possible with the clean and site. Instead you have to execute a specific build phase within the default build life cycle.
Build Phase
Description
validate
Validates that the project is correct and all necessary information is available. This also makes sure the dependencies are downloaded.
compile
Compiles the source code of the project.
test
Runs the tests against the compiled source code using a suitable unit testing framework.
package
Packs the compiled code in its distributable format, such as a JAR.
install
Install the package into the local repository.
deploy
Copies the final package to the remote repository for sharing.
Project Dependencies
Your project may need external Java APIs or frameworks which are packaged in their own JAR files to be on the classpath when you compile your project code. Each external JAR may again also need other external JAR files etc. Downloading all these dependencies (JAR files) recursively downloaded is cumbersome.
Maven has built-in dependency management. You specify in the POM file what external libraries your project depends on, and which version, and then Maven downloads them for you and puts them in your local Maven repository. If any of these external libraries need other libraries, then these other libraries are also downloaded into your local Maven repository.