java configure MyEclipse Maven environment
Although most of my projects have been moved to Idea, I still habitually click MyEclipse when writing some small test programs. When I used third-party libraries, I would get used to downloading jar packages and then importing them with build paths. However, in idea, it seems that it is more convenient to configure maven dependencies. So I also want to use pom.xml to import dependencies in MyEclipse. I encountered some problems during the attempt. I am here to record the solution to these problems.
environment
Myeclipse for spring 2014
JRE 8
Maven 3.3.3 (Although MyEclipse comes with mvn plugin, I have installed mvn before and don't know which one is used in the middle process)
I won't introduce the functions of mvn and the format of pom.xml. I will directly enter the Ask&&Answer link below
1 Report an error: pom.xml format is incorrect
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test-project</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9</version> </dependency> </dependencies> </project>
This is the simplest pom.xml that contains all the necessary elements.
-xml header
-project attribute information
-modelVersion
-The product's groupId, artifactId and version
When configuring a dependency (the jar package you depend on) you need to include at least three elements
-groupid
-artifactId
-version
and put the elements in the dependency tag and include them in the dependencies tag
2 I don't know how to fill in the dependency configuration items of the package I depend on
There are some websites online that search for maven configuration information
I use this now
http://mvnrepository.com/
Just search directly with whatever you need. The success rate of using multiple keywords is relatively high, such as com.google.gson searches in com.google and gson, and you will get the results you want in the end
3 What should I do if I fill in the dependency but still report an error saying that I can't download it
For example, relying on the json lib package, we know that we need to fill in the following information through searching, but it is impossible to use it if we put it directly into pom.xml.
<dependency> <span style="white-space:pre"> </span><groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency>
Through other ways, we can know that this jar package has other dependencies and can only be used in jdk5. Therefore, if the current jdk version is different, it cannot be downloaded. Therefore, we need to configure it in the following form
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency>
This kind of information can only be solved by looking for maven dependencies of the specified package online, and there is no universal solution.
4 I have configured pom.xml, so how do we import these jar dependencies
In MyEclipse, there is a difference between ordinary projects and maven projects. If you want to use maven to manage dependencies and generate products, you need to build a maven project, but this operation project is completely different from our operations in idea, which is also very different from our understanding (I need maven to just help me download the dependency jar package, but in other cases I don't need it).
There is an alternative solution here. If we originally had an ordinary project, we now need to use mvn to manage the dependencies of the part. At this time, we can convert the original project into a maven project, and then use the Debug As->Maven Install method to install the library file. After the conversion is completed, we can still compile and export the file in the previous way. Unlike the previous one, we can add dependencies through pom.xml.
The conversion method is to right-click the name of the project -> click Configure->convert to maven project in the right-click menu
Other questions
5 When running the project prompts that the main method cannot be found.
One possible reason to view project -> properties->Java build path
Check whether to add the root directory where the current Java file is located to the source (default is the src folder)
6 Garbage appears
This situation generally occurs when MyEclipse defaults to inherit the system's default character set. This character set will generally become GBK under Windows, but the internationally common character set specification is UTF8.
This situation can be solved in two ways
Set character set for the current directory
project -> properties->resource
Modify Text file encoding to UTF8
Modify MyEclipse default character set
windows->preference->general->editors->text editors->spelling
Modify encoding to UTF8
7java8 support
Java 8 has been out for several years, but the support for Java 8 in the current market environment is still not friendly. Although I think the syntax of lambda is really good (but it is really a bit troublesome to get started and you need to be familiar with the syntax), Myeclipse for Spring 2014 only supports Java 7. If you need to use Java 8, you need to upgrade to MyEclipse 2015 GA and later versions, of course you can also switch to idea or eclipse camp (laughs)
The above is all content
Thank you for reading, I hope it can help you. Thank you for your support for this site!