Maven project problems in eclipse, InteliJ IDEA, mavenintelij

Source: Internet
Author: User

Maven project problems in eclipse, InteliJ IDEA, mavenintelij

Please indicate the source for reprinting. Thank you!

No matter what ide we use to edit our code, the final product will run out of the ide. Just as yanyfei leaves the nest, as you leave your home, it will run unexpectedly.

-Singles' Day January 26, 2018

Have you ever thought about:

Question 1: Is the maven project running normally in eclipse, but there is no pointer in InteliJ IDEA? Error?

Question 2: Why can code in src/test/java import classes in src/main/java, but not vice versa?

Answer 1: The compiling rules of each ide are different.

Answer 2: The maven lifecycle is determined.

Solution to the root cause: Put down the ide, pay attention to the maven itself, and return to the maven root directory to execute the maven command (although the ide has already integrated some maven commands ).

After seeing my answers, you may have gotten to my point when you are browsing this page. However, based on the principle of beginning and end, I still want to continue:

By default (the compilation time can be changed by skilled users ):

The compile notes for saving eclipse are-> Ctrl S

Compile InteliJ IDEA during Runtime as-> Runtime

This determines that InteliJ IDEA is closer to maven during compilation (in fact, InteliJ IDEA compilation does not fully comply with maven specifications ).

We can use the mvn help: Valid tive-pom command to check that only the compile and test-compile sections are captured.

We can see that the order of maven compilation is to first compile src/main/java, and then compile src/test/java;

This explains Question 2-> the code in src/test/java can import classes in src/main/java, but not vice versa; because the classes in src/test/java have not been compiled, you cannot import them. Otherwise, an error is returned.

However, if eclipse is used for editing, the code in src/main/java can reference classes in src/test/java. The reason is already given above, do not repeat it again (if you cannot find any friends, please leave a message ).

The above describes the disadvantages of editing maven projects in eclipse. The InteliJ IDEA students should not be gloated. I will refer to an error that cannot be detected when InteliJ IDEA is edited in maven.

Classes in src/main/java

 

Junit3 is used because it is automatically generated when I create maven. I am used to using the @ Test annotation to import the junit4 + package;

Directory structure and the code in App and AppTest. You can see that App objects are introduced to AppTest under the src/test/java directory.

There is nothing different here;

Next, we will add the following code in pom. xml: (overwrite the execution and Compilation part in executive tive-pom)

<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 there is no exception in InteliJ IDEA. When we run the test case, we find that the operation is normal and the four directories under the target directory are generated. This is automatically compiled during IDEA's Runtime:

Run mvn clean test-compile.

 

Error:

At this time, because we modified the pom. xml file. As a result, the code in src/main/java is not compiled, and the code in src/test/java cannot access the classes in src/main/java.

IDEA's built-in compilation effect is declared to be inconsistent with maven's requirements. IDEA does not care about the plug-in pom. xml for compilation.

Conclusion: The maven project will eventually be compiled by returning to the maven command, so do not rely on ide compilation. Execute mvn clean package before the transfer or release. If success is used, congratulations, this code runs properly under any ide (so don't ask me why my code runs normally in eclipse, but it cannot run in InteliJ IDEA, through this article, you should know that even if the IDEA can run normally, it may not be in line with maven's intention ).

Git demo

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.