Maven Best Practices

1. Specify the vm parameter in eclipse.ini

Eclipse runs on JRE by default, and some functions of m2eclipse require the use of JDK. The solution is to configure the eclipse.ini file in the Eclipse installation directory and add the vm configuration to point to the JDK, such as:

--launcher.XXMaxPermSize 256m
-vm
D:\java\jdk1.6.0_07\bin\javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.5
-Xms128m
-Xmx256m

Second, set the MAVEN_OPTS environment variable

We usually need to set the value of MAVEN_OPTS to: -Xms128m -Xmx512m, because the default maximum available memory of Java is often not able to meet the needs of Maven running. For example, when the project is large, using Maven to generate the project site needs to take up a lot of memory. With this configuration, we easily get java.lang.OutOfMemeoryError. Therefore, configuring this variable at the outset is the recommended practice.
For how to set the environment variable, please refer to the previous method of setting the M2_HOME environment variable, and try not to directly modify the two Maven execution script files, mvn.bat or mvn. Because if you modify the script file, you have to modify it again when you upgrade Maven, which is troublesome and easy to forget. In the same way, we should try not to modify any files in the Maven installation directory.

Third, configure the user scope settings.xml

Maven users can choose to configure $M2_HOME/conf/settings.xml or ~/.m2/settings.xml . The former is global, and all users on the entire machine are directly affected by the configuration, while the latter is user-wide, and only the current user is affected by the configuration.

We recommend using user-scoped settings.xml, mainly to avoid unintentionally affecting other users in the system. Of course, if you have real needs, you need to unify the settings.xml configuration of all users in the system, of course, you should use the global scope settings.xml.

In addition to affecting scope, configuring the user-scoped settings.xml file also facilitates Maven upgrades. Directly modifying the settings.xml in the conf directory will cause inconvenience to Maven upgrade. Every time you upgrade to a new version of Maven, you need to copy the settings.xml file. If you use the settings.xml in the ~/.m2 directory, it will not affect the Maven installation file, you do not need to touch the settings.xml file when upgrading.

4. Do not use Maven embedded in IDE

Whether it is Eclipse or NetBeans, when we integrate Maven, an embedded Maven will be installed. This embedded Maven is usually relatively new, but not necessarily very stable, and it is often different from the Maven we use on the command line. the same version. There are two potential problems here: First, there are many unstable factors in the newer version of Maven, which can easily cause some incomprehensible problems; secondly, in addition to IDE, we also often use Maven from the command line, if the versions are inconsistent , it is easy to cause inconsistent build behavior, which we do not want to see. Therefore, we should use Maven consistent with the command line when configuring the Maven plugin in the IDE.

In the m2eclipse environment, click Windows in the menu bar , then select Preferences , in the pop-up dialog box, expand the Maven item on the left, select the Installation sub-item, in the right panel, we can see that there is a default Embedded  Maven The installation is selected, click Add... Then select our Maven installation directory M2_HOME, and select this external Maven after adding.

 No picture here. . .

 Figure 2-14 Using external Maven in Eclipse

The NetBeans Maven plugin will detect the PATH environment variable by default, so it will directly use the Maven environment consistent with the command line. Click ToolsOptionsOtherMaven tab in the menu bar, and you can see the configuration shown in Figure 2-15:

 


 Figure 2-15 Using external Maven with NetBeans

 

五、maven warnning 'build.plugins.plugin.version' is missing

Add new dependency in pom.xml:

 <dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<type>maven-plugin</type>
</dependency>
在build中添加:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven.compiler.plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<verbal>true</verbal>
</configuration>
</plugin>
</plugins>
</build>
</project>

但是仍然有个错误,错误信息:build.plugins.plugin.version
可以看到,在pom.xml里面的<build>下面,沒有<version>。因此要多加一个version:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven.compiler.plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<verbal>true</verbal>
</configuration>
</plugin>
</plugins>
</build>
</project>

好,解决问题。

六、如何去掉maven的[WARNING] Using platform encoding (UTF-8 actually) to copy filter

  1. <project>  
  2.   ...  
  3.   <properties>  
  4.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  5.   </properties 
  6.   ...  
  7. </project>  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326609248&siteId=291194637