How to Read and Write a Java File in Maven Properties
In lodge to understand the difficulty when we do not use the maven in Coffee project, there are some tasks that we have to do:
- re-create resources such as conf files to the build folder.
- compile the source files.
- copy dependency jars to build folder.
- run unit tests.
- package the build folder every bit jar.
To make our life easier to jiff, Maven was created.
Maven is a tool for managing resource objects or packet in Java. It is as same as npm in Node.js. When you want to build a enormous projection, absolutely, you need maven.
Understanding some tricky things about maven will help you lot easily to write lawmaking, use so many packages finer. Y'all do not manually add together many packages into your project. To configure maven, you will cope with agreement some parameter in POM file.
In this article, we will find informations about configuring POM file.
Table of Contents
- Standard structure in Maven project
- POM file in Maven projection
- Add specific bundle into project through pom file
- How the Maven works
- Wrapping up
Standard structure in Maven project
The beneath is the prototype about standard folder construction in maven project.
your-app-proper name |-- pom.xml `-- src |-- main | |-- java | | `-- com | | `-- your-visitor-proper name | | |-- config | | | |-- Config.coffee | | | `-- WebInitializer.java | | | | | `-- controllers | | `-- Controller.java | |-- resources | `-- webapp | `-- Spider web-INF | `-- jsp | `-- alphabetize.jsp `-- examination `-- coffee `-- com `-- your-company-proper name `-- app `-- AppTest.java
The followings is the table explains the meanings of each path in your folder structure.
Path | Meanings |
---|---|
src/primary/java | Application/Library sources |
src/main/resources | Application/Library resource |
src/main/webapp | Web awarding sources |
src/main/filters | Resources filter files |
src/test/java | Examination sources |
src/test/resources | Resources is used for unit of measurement tests, and information technology will not exist deployed |
src/test/filters | Test resource filter files |
src/it | Integration tests |
src/site | Files are used to generate the Maven project |
At the superlative level, files descriptive of the project is a pom.xml file.
There are just ii sub folders: src and target. The target directory is used to contains all output of the build. The src directory contains all the source material for building the project, its site so on.
POM file in Maven project
A POM - Projection Object Model is an xml files that contains all informations about project and configuration details used past Maven to build the project.
All our information will be contained in project tag. To be default, some parameters of project volition be inserted in pom.xml.
<projection ... > <modelVersion>4.0.0</modelVersion> <!-- POM relationships --> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <parent>...</parent> <dependencyManagement>...</dependencyManagement> <dependencies>...</dependencies> <modules>...</modules> <!-- Project information --> <name>...</proper noun> <description>...</clarification> <url>...</url> <inceptionYear>...</inceptionYear> <licenses>...</licenses> <developers>...</developers> <contributors>...</contributors> <system>...</organisation> <!-- Build Settings --> <packaging>...</packaging> <properties>...</properties> <build>...</build> <reporting>...</reporting> <!-- Build Environment --> <!-- Environment Data --> <issueManagement>...</issueManagement> <ciManagement>...</ciManagement> <mailingLists>...</mailingLists> <scm>...</scm> <!-- Maven Environment --> <prerequisites>...</prerequisites> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <distributionManagement>...</distributionManagement> <profiles>...</profiles> </projection>
With:
- groupId: a name of your visitor, …
- artifactId: a name of package, project
- version: a version of project
- packaging: in that location are 4 values: ear, pom, jar (default value) and war. jar - library or java awarding; state of war - web awarding.
- modelVersion: its value will exist e'er 4.0.0 for Maven 2.ten POMs.
Note about jar, state of war files:
- .jar files: The .jar files comprise libraries, resources and accessories files like belongings files.
- .war files: The state of war file contains the web awarding that can be deployed on any servlet/jsp container. The .state of war file contains jsp, html, javascript and other files necessary for the development of spider web applications.
Note:
-
<version>1.0.0-SNAPSHOT</version>
: TheSNAPSHOT
version suffix tells Maven that this projection is currently in evolution. It has implications for the way artifacts are handled by the dependency resolution. -
artifacts
: Maven artifacts belong to a group (typicallycom.company.awarding
), and must have a unique identifier (typically the application's name). - The master repository for Maven is called the Cardinal Repository and tin be searched on at
http://maven.org
.
Add specific packages into project through pom file
In order to import many libraries into our project through Maven, insert informations nigh these libraries into dependencies tag in pom.xml.
properties tag helps our define many abiding variables, make our do non concern nearly some problems when we alter this value at the other places. Information technology makes our lawmaking synchronous. Information technology will used to tell Maven utilize a specified JDK version to compile the source code.
Belows are the definitions of some common packages that will use for Spring MVC.
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.viii</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <jump.version>4.0.0.RELEASE</spring.version> <jstl.version>one.2</jstl.version> <javax.servlet.version>3.0.1</javax.servlet.version> <!-- Spring --> <spring-framework.version>4.0.2.RELEASE</bound-framework.version> </properties> <dependencies> <!--Apply guava library--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.0.1-jre</version> <!-- or, for Android: --> <version>27.0.1-android</version> </dependency> <!--hibernate dependencies--> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.5.Final</version> </dependency> <dependency> <groupId>org.hide</groupId> <artifactId>hibernate-cadre</artifactId> <version>4.three.eleven.Concluding</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.11.Terminal</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hide-jpa-2.1-jpa</artifactId> <version>1.0.0.Final</version> </dependency> <!--use Log4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency> <!--jquery, bootstrap from webjars--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.seven</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>1.12.4</version> </dependency> <!--Spring and transactions--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>bound-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!--Spring JDBC support--> <dependency> <groupId>org.springframework</groupId> <artifactId>jump-jdbc</artifactId> <version>${bound.version}</version> </dependency> <!--MySQL commuter--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.5</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>7.0</version> <blazon>jar</blazon> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>leap-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${leap.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- junit 5, unit of measurement examination --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.3.one</version> <scope>test</scope> </dependency> <!-- unit test --> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> <scope>examination</telescopic> </dependency> <!-- for spider web servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet.version}</version> <scope>provided</telescopic> </dependency> <!-- use for SHA hashing --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>i.11</version> </dependency> <!-- utilise for hibernate --> <dependency> <groupId>hide</groupId> <artifactId>hibernate</artifactId> <version>three.0.3</version> <scope>compile</telescopic> </dependency> </dependencies>
scope tag will help our restrict the usage of libraries. Because some libraries can be used for unit testing, the other can be provided at runtime by the awarding server.
Maven provides four dependency scopes:
- compile: A compile-scope dependency is available in all phases. Information technology is the default value.
- provided: A provided dependency is used to compile the awarding, but will not exist deployed. You would use this scope when you expect the JDK or application server to provide the JAR. For instance, Servlet. provided dependencies are bachelor on the compilation classpath (non runtime). They are non transitive, nor are they packaged.
- runtime: Runtime-scope dependencies are non needed for compilation, merely for executing and test the system, such as JDBC drivers.
- exam: Test-scope dependencies are needed only to compile and run tests. For instance, JUnit.
-
system: Information technology has backdrop as same as provided, except that you have to provide an explicit path to the JAR on the local file system.
This is intended to allow compilation against native objects that may be function of the system libraries. The antiquity is assumed to ever be available and is not looked up in a repository. If you declare the scope to exist organisation, you must also provide the systemPath chemical element. Note that this telescopic is not recommended (y'all should always try to reference dependencies in a public or custom Maven repository).
- import: It is used to import some dependency but it must be pointed in <dependencyManagement\/> tags in POM files.
How the Maven works
Firstly, we add informations about libraries that we need into dependencies tag, then, we save the pom.xml file. Maven will ship these informations to its cloud repository. Next, information technology will download these libraries into local repository of Maven.
To check the path of this local repository, you can open User setting of Netbeans or Eclipse. Finally, Maven will add the path of each libraries into Dependencies folder in Netbeans.
And Leap Boot Maven Plugin provides some user-friendly features:
-
It searches for the
public static void main()
method to flag every bit a runnable form. -
It provides a built-in dependency resolver that sets the version number to match
Leap Boot dependencies
. You tin can override any version you wish, but it volition default to Boot's chosen set of versions.
Some problems when using Maven
-
Unable to notice valid certification path to requested target
When using command line mvn clean install for our project, we volition encounter a trouble that look at an below image.
The cause of this problem:
-
This is due to the fact that we want to access the server
http://repo.maven.apache.org/maven2
, but Java'south security policy does not find some certificates in its security files of a folder %JAVA_HOME%\jre\lib\security.So, it will throw an exception.
Solution for this trouble:
-
In this instance, we volition use keytool that is provided past JDK.
-
To list all certificates in cacerts file, we can apply the following command.
keytool - list - keystore cacerts
When we cope with the prompt
Enter keystore countersign
, it's the first time that we set up countersign for this keytool. And then we only type our countersign for it. -
Then, we will connect to the server that nosotros desire to become certificates. In our current case, it is
http://repo.maven.apache.org/maven2
.keytool - printcert - rfc - sslserver repo . maven . apache . org / maven2
After typing this control line in cmd window, we need to save the content of these certificates in a file that is in the aforementioned binder with cacerts file.
Assuming that nosotros named this file that is
mpdcertificates.pem
file. -
Import these above certificates into cacerts file.
keytool - importcert - file ./ mpdcertificates . pem - keystore ./ cacerts
-
-
The specified user settings file does non exist:
C:\Users\hullo\.m2\settings.xml
Past default, Maven does non create the settings.xml file nether .m2 binder. To solve this problem, we can copy the settings.xml file from folder
%MAVEN_HOME%\conf
to .m2 folder. -
Fix upward the Maven Archetype Catalogs plugin
-
Go to File -> Settings -> Plugins -> Browse, and so we accept:
Select File -> Invalidate Caches/Restart … item.
-
So, go to File -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven Archetype Catalogs.
Discover a archetype-catalog.xml file in C:\Users\hi.m2\repository folder.
-
Wrapping up
- Understanding virtually structure folder in Maven project.
- The important parameters in POM file such as parent, packaging, dependencies, properties, name, scope, …
- How to add libraries into our project with Maven.
- Necessary parameters for mutual libraries in Spring MVC.
Refer:
POM file
http://world wide web.codetab.org/apache-maven-tutorial/
https://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-pom-syntax.html
http://www.java2s.com/Tutorials/Java/Maven_Tutorial/1020__Maven_POM_File.htm
https://o7planning.org/vi/10411/quan-ly-cac-phu-thuoc-trong-maven
https://o7planning.org/vi/10131/huong-dan-su-dung-maven-cho-nguoi-moi-bat-dau
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
https://www.javaworld.com/article/2071772/java-app-dev/the-maven-2-pom-demystified.html
https://world wide web.javaworld.com/article/2072203/build-ci-sdlc/an-introduction-to-maven-2.html
JAR, State of war files
https://en.wikipedia.org/wiki/WAR_(file_format)
https://en.wikipedia.org/wiki/JAR_(file_format)
https://web.archive.org/spider web/20120626012843/http://java.sunday.com/developer/Books/javaprogramming/JAR/basics
https://web.archive.org/web/20120626020019/http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
http://www.vogella.com/tutorials/ApacheMaven/article.html
Projection Dependecies https://books.sonatype.com/mvnref-book/reference/pom-relationships-sect-projection-dependencies.html#pom-relationships-sect-dependency-scope
Setup maven in eclipse
https://o7planning.org/6/10101/cai-dat-maven-vao-eclipse
Create empty maven project in eclipse
https://o7planning.org/vi/11253/tao-maven-web-app-projection-rong-tren-eclipse
https://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art032
Source: https://ducmanhphan.github.io/2019-01-20-Configure-maven-pom-xml-in-spring-mvc/
0 Response to "How to Read and Write a Java File in Maven Properties"
Post a Comment