7

Per l'applicazione di avvio Spring con tomcat incorporato, a causa di alcune limitazioni, è necessario eliminare spring-boot-maven-plugin e utilizzare maven-shade-plugin. Con il comando maven package ho potuto creare con successo il file jar. Tuttavia, tutti i miei controller hanno smesso di funzionare e mi danno errore 404. Anche i contenuti statici inseriti nella mia cartella delle risorse non vengono più pubblicati. Sempre ricevendo errore 404.Avvio a molla con plug-in Maven Shade - Controller non mappati (errore 404)

mia pom.xml

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <!-- Import dependency management from Spring Boot --> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-dependencies</artifactId> 
      <version>1.2.7.RELEASE</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.7</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 

     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
      </configuration> 
     </plugin> 


     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 

      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
        <configuration> 
         <filters> 
          <filter> 
           <artifact>*:*</artifact> 
           <excludes> 
            <exclude>META-INF/*.SF</exclude> 
            <exclude>META-INF/*.DSA</exclude> 
            <exclude>META-INF/*.RSA</exclude> 
           </excludes> 
          </filter> 
         </filters> 

         <transformers> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
           <mainClass>abc.MyMainClass</mainClass> 
          </transformer> 
         </transformers> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 


    </plugins> 
</build> 

ho eseguire l'applicazione come al solito:

@SpringBootApplication 
public class MyMainClass{ 

    public static void main(String[] args) { 
     SpringApplication.run(MyMainClass.class, args); 
    } 
} 

Inoltre ho dovuto creare a seguito di fagioli EmbeddedServletContainerFactory, altrimenti verrà generato un errore.

@Bean 
public EmbeddedServletContainerFactory servletContainer() { 
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); 
    return tomcat; 
} 

Infine, ho solo un controller di un campione,

@RequestMapping(value = "/car", method = RequestMethod.GET) 
@ResponseBody 
public Car test() { 
    return new Car(); 
} 

Questo stesso codice funziona senza alcun problema quando uso plug-primavera-boot-Maven. Poco dopo aver iniziato a usare maven-shade-plugin, tomcat si avvia correttamente. tuttavia, tutti i controller mi danno errore 404. Per favore guidami se sto facendo qualcosa di sbagliato.

+0

Sarebbe utile se qualcuno in grado di fornire un esempio di lavoro di primavera Boot con Maven plugin per l'ombra. Grazie. –

risposta

8

E 'molto semplice, se si vuole migrare da plug-in primavera-boot-Maven per plugin maven-shade. Ho aggiunto le seguenti informazioni sul plugin a pom.xml e il problema è stato risolto.

(https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.2.6.RELEASE/spring-boot-starter-parent-1.2.6.RELEASE.pom)

gestione

Dipendenza:

<dependencyManagement> 
<dependencies> 
    <dependency> 
     <!-- Import dependency management from Spring Boot --> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-dependencies</artifactId> 
     <version>1.2.7.RELEASE</version> 
     <type>pom</type> 
     <scope>import</scope> 
    </dependency> 
</dependencies> 

costruire plugin:

<build> 


    <plugins> 


     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
      </configuration> 
     </plugin> 


     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <dependencies> 
       <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
        <version>1.2.7.RELEASE</version> 
       </dependency> 
      </dependencies> 
      <configuration> 
       <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope> 
       <createDependencyReducedPom>true</createDependencyReducedPom> 
       <filters> 
        <filter> 
         <artifact>*:*</artifact> 
         <excludes> 
          <exclude>META-INF/*.SF</exclude> 
          <exclude>META-INF/*.DSA</exclude> 
          <exclude>META-INF/*.RSA</exclude> 
         </excludes> 
        </filter> 
       </filters> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
        <configuration> 
         <transformers> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
           <resource>META-INF/spring.handlers</resource> 
          </transformer> 
          <transformer 
           implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer"> 
           <resource>META-INF/spring.factories</resource> 
          </transformer> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
           <resource>META-INF/spring.schemas</resource> 
          </transformer> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
           <mainClass>sg.butterfly.emenu.api.config.EmenuApp</mainClass> 
          </transformer> 
         </transformers> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 

</build> 
2

Oltre a quella già esistente, provare a utilizzare AppendingTransformer, ad esempio:

<transformer 
           implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
           <mainClass>abc.MyMainClass</mainClass> 
          </transformer> 
<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> 
           <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
       <resource>META-INF/spring.tooling</resource> 
           </transformer> 

Fare riferimento Doc: https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

+0

Grazie per la rapida risposta .. Ho provato e questo non funziona. Sembra che il modo in cui eseguo la migrazione da spring-boot-maven plugin a maven-shade-plugin sia sbagliato. Hai bisogno di capire come configurarlo correttamente. –