Reprint please indicate the source, thank you!
No matter what IDE we use to edit our code, the final product will run out of the IDE, just as feier away from the nest, as you leave the home, and the problem will run out of time.
-Tanqiang January 26, 2018 11:53
Have you ever thought about:
The problem 1:maven project runs normally in Eclipse, but in Intelij idea, a null pointer? Error?
Question 2: Why does the code in Src/test/java import the classes in Src/main/java, and vice versa?
Answer 1: The compilation rules for each IDE are different.
Answer the life cycle of the 2:maven decision.
Solution to the root cause: Drop the IDE, focus on Maven itself, and go back to the MAVEN root directory to execute the MAVEN command (although the IDE has integrated some MAVEN commands).
See my answer, is browsing this page you may have got to my point, but in line with the principle of beginning and ends, I still want to continue:
By default (friends who are skilled in using can change the compile time):
Compile précis-writers with Ctrl S when Eclipse is saved
Intelij Idea Run-time compilation précis-writers to runtime
This determines that the Intelij idea is closer to Maven (in fact, the compilation of Intelij idea does not exactly conform to MAVEN's specification) in the compilation effort.
We can take a look at the MVN help:effective-pom command and intercept only the compile and test-compile parts.
It can be seen that the compilation sequence of Maven is first compiled Src/main/java, after compiling Src/test/java;
This explains that the code in Src/test/java, Problem 2, can import the class in Src/main/java, and vice versa, because the class in Src/test/java has not been compiled, so you cannot import it, otherwise you will get an error.
But if you use Eclipse Edit, the code in Src/main/java can refer to the class in Src/test/java, the reason is given above, no longer repeat (not found friends welcome message).
These are the drawbacks of the Eclipse editing Maven project, and Intelij idea's classmates don't gloat, I'm going to give you an error Intelij the idea when you edit maven.
Classes under the Src/main/java
Use JUNIT3, because it is automatically generated when I create MAVEN, so use, accustomed to use @test annotation classmate can import junit4+ package;
directory structure and the code in App,apptest, you can see that the apptest in the Src/test/java directory introduces an App object.
There is nothing different about it;
Below we add the following code in Pom.xml: (Overwrite the part of the Effective-pom that performs the compilation)
<plugin> <Artifactid>Maven-compiler-plugin</Artifactid> <version>3.1</version> <executions> <Execution> <ID>Default-compile</ID> <Phase>Clean</Phase> <Goals> <goal>Help</goal> </Goals> </Execution> <Execution> <ID>Default-testcompile</ID> <Phase>Test-compile</Phase> <Goals> <goal>Testcompile</goal> </Goals> </Execution> </executions></plugin>
We can see that Intelij idea is not unusual, running the test case, discovering that it is working properly, and generating the corresponding four directories under the target directory, which is compiled automatically when the idea Runtime:
We re-execute: mvn clean test-compile
Error:
At this point, because we have modified the Pom.xml file, the code in the compilation phase Src/main/java is not compiled, and the code in Src/test/java does not have access to the classes in Src/main/java.
Idea's own compilation effect announced that it does not match Maven's requirements, and idea does not care about plugin in Pom.xml to compile.
Conclusion: The MAVEN project will eventually return to the MAVEN command to compile, so do not rely on the IDE's compilation; Please execute MVN clean package before transferring or publishing, if success, then congratulations, This code will run correctly under any of the Ides (so friends don't ask why my code works in Eclipse, and Intelij idea doesn't work, and you should know that even if it works correctly, you can may not be compatible with MAVEN's intentions).
Some problems of MAVEN project in Eclipse,intelij idea