Maven best practices

Time:2021-3-14

Maven is an excellent project management tool. At present, the best book on the market is Maven.
Still can’t solve the problem:
1. Maven built-in variable
Maven built in variable description:
${basedir} project root
${ project.build.directory }Build a directory. The default is target
${ project.build.outputDirectory }The output directory of the construction process is target / classes by default
${ project.build.finalName }Output name, default to${ project.artifactId }-${ project.version }Current version
${ project.packaging }The package type is jar by default
${ project.xxx }The contents of any node in the current POM file



<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>cn.com.travelman</groupId>
  <artifactId>CommonRestService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestService</name>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
   <dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
         <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ServiceData</artifactId>
        <version>${project.version}</version>
        </dependency>
  </dependencies>
  <build>
   <plugins>
         <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <configuration>  
          <source>1.7</source>  
          <target>1.7</target>  
        </configuration>  
      </plugin> 
  <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <configuration>  
                    <archive>  
                        <manifest>  
                            <addClasspath>true</addClasspath>  
                            <mainClass>cn.com.travelman.service.rest.publish.Server</mainClass>  
                        </manifest>  
                    </archive>  
                </configuration>  
            </plugin>  
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
 </plugins>
  </build>
</project>

An important feature of Maven is that it can write the value of classpath to the MF file of jar. This allows you to run the jar file directly.
You can do this with Maven jar plugin. < addclasspath > true < / addclasspath > means that the dependent package path is written to the MF file. But note that the classpath you write is just the name of the package, so the jar package you run should be in the same directory as the Lib package.

Maven can introduce another project into one project

 <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ServiceData</artifactId>
        <version>${project.version}</version>
        </dependency>

Yes, artifactid is the artifactid of your other project. Is this much more convenient than using java build path to reference?

And use Maven dependency plugin to export these packages to the specified directory.

Besides, gohttp://mvnrepository.com/Search for the configuration of the desired package.

Another problem: these configurations are not what you want. For example, search Apache CXF.
At this time, go to the official website to download sample and find the POM file configuration.

How to configure multiple source files for Maven:
http://casheen.iteye.com/blog/540385