maven --- The "Canal" in Packaging

Keywords: Programming Java Apache Maven Spring

Since taking over the project with maven, packing has not been done manually.

1: Make a jar bag.

There are many ways to make jar packages with maven. Here are two ways I have used:

  1. assembly, add the following code in pom.xml:
     <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>2.2</version>
           <configuration>
               <archive>
                   <manifest>
                       <!--Represents the system class of your system, that is main Classes of methods-->
                       <mainClass>com.byd.Start</mainClass>
                   </manifest>
               </archive>
               <descriptorRefs>
                   <descriptorRef>
                       jar-with-dependencies
                   </descriptorRef>
               </descriptorRefs>
           </configuration>
           <executions>  
                <execution>  
                    <id>make-assembly</id>  
                    <phase>package</phase>  
                    <goals>  
                        <goal>single</goal>  
                    </goals>  
                </execution>  
            </executions>  
       </plugin>

The package will contain all the jar packages, but I've encountered them here.

First question: My project is done with Springmvc, so I used javaee-api-7.0.jar, but there is no complete reference to specific classes in the project. Maybe the association in a class used the classes in this jar, so I have been reporting errors. I can't find one of the classes in javaee-api-7.0.jar. Later, it was found that the scope of javaee-api-7.0.jar introduced by maven was procided: this means that it can be packaged without being packaged, and other facilities (web container) will provide it. In fact, this dependency can theoretically participate in compilation, testing, and running cycles. It's equivalent to compile, but exclude is done in the packaging phase. So change scope to compile, and this problem is solved.

Next, continue to run jar and find or report an error. The content of the error is as follows:

1. Cannot read the xml configuration file;

2. Seemingly read the xml file, exception org. spring framework. beans. factory. NoSuchBean Definition Exception, in fact, the xml file has not been correctly read and parsed;

3. Packet conflict;

Solution: Use other plug-ins to package the shade plug-in, pom.xml to reference the code

        <plugin>  
           <groupId>org.apache.maven.plugins</groupId>  
           <artifactId>maven-shade-plugin</artifactId>  
           <version>2.4.1</version>  
           <executions>  
               <execution>  
                   <phase>package</phase>  
                   <goals>  
                       <goal>shade</goal>  
                   </goals>  
                   <configuration>  
                       <transformers>  
                           <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                               <!--Your main Class-->
                               <mainClass>com.byd.Start</mainClass>  
                           </transformer>
                                <!--To prevent spring Of xml Configuration read error, these two lines must be added-->
                           <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                               <resource>META-INF/spring.handlers</resource>  
                           </transformer>  
                           <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                               <resource>META-INF/spring.schemas</resource>  
                           </transformer>  
                       </transformers>  
                   </configuration>  
               </execution>  
           </executions>  
       </plugin>

OK, this problem is solved.

So far, we have basically solved the problem of packaging Spring MVC projects into jar packages to run. Next, we need to package a real web project into war packages. Let's record a few points. My project is spring boot project:

1,<packaging>war</packaging>.

2. Several main plug-ins are as follows:

            <plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<useSystemClassLoader>false</useSystemClassLoader>
				</configuration>
			</plugin>
			<!-- Support maven Packing Custom Name  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>BYD_PAS</warName>
                </configuration>
            </plugin>
           

When the package is finished, the war package is put under tomcat and an error is found. The error code is as follows

serious: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Product]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)
//April 17, 2018: 55:43 a.m. org. apache. catalina. startup. HostConfig deployment Directory
//Serious: Error deploying web application directory H: apache-tomcat-7.0.52-windows-x64 apache-tomcat-7.0.52 webapps Product
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Product]]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)

It was found that the mismatch between tomcat package and spring boot version of related packages might lead to the problem solving after tomcat was upgraded from 7.0.85 to 9.0.65.

Posted by kparish on Mon, 05 Aug 2019 00:45:34 -0700