“unable to locate Spring NamespaceHandler” error

前端 未结 5 714
后悔当初
后悔当初 2020-12-09 17:26

I\'m creating a stand-alone Sava application with Spring, to handle the JDBC access. The application works fine on every test and I decided that I need a jar to be deployed

相关标签:
5条回答
  • 2020-12-09 18:05

    In addition to the solution posted by @GrampaJohn, I also had to make sure that I was packaging the required libraries into the generated JAR file instead of extracting them into the generated JAR.

    In Eclipse Mars, after making changes to the pom.xml (added maven-shade plugin as suggested by @GrampaJohn), follow the steps:

    File -> Export -> Select Java folder -> Runnable JAR File -> enter the Launch configuration (main file), export destination, and in Library Handling, select "Package required Libraries into generated JAR -> Click finish

    .

    0 讨论(0)
  • 2020-12-09 18:07

    Looks like a bug in the Maven Assembly Plugin - MASSEMBLY-360, and as discussed in this blog entry here.

    In short, the meta-data files in the Spring JARs that handle the Spring namespaces are being mangled by maven.

    0 讨论(0)
  • 2020-12-09 18:10

    I ran into this problem today with the maven-assembly-plugin. A search brought me to this question, and a look a the bug report suggested that perhaps I was using the wrong plugin. So I switched to the maven-shade-plugin. It works perfectly, as far as I can tell. I have an executable jar that incorporates a number of Spring modules as well as Apache MQ. Here's the relevant bit from my pom.xml:

    <build>
    <finalName>sample-broker</finalName>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <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.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.XYZ</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    </build>
    
    0 讨论(0)
  • 2020-12-09 18:12

    I found the error, the bug lies in an unfixed bug in the maven-assembly plugin. I used the following workaround:

    First commented out the maven-assembly code in my pom. Then I copied the dependencies to a lib folder at the target using the maben-dependency-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>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Then I used the maven-jar-plugin to setup my executable jar:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>org.foo.myproject.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <mode>development</mode>
                            <url>${pom.url}</url>
                            <key>value</key>
    
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
    

    Finally I created a bash script that is deployed with the application that runs my app with its libs and any provided arguments:

    java -cp lib/*:myproject-0.0.1-SNAPSHOT.jar org.foo.myproject.App $@
    

    I should have built the app in python =/

    0 讨论(0)
  • onejar-maven-plugin lets you resolve maven and spring contradiction by creating a single jar file with dependencies. It creates a wrapper around your jar file by placing it inside another jar (one-jar).

        <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                  <execution>
                    <goals>
                        <goal>one-jar</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>your.package.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
    
    <!-- outside build tag in pom.xml -->
    <pluginRepositories>
            <pluginRepository>
                <id>onejar-maven-plugin.googlecode.com</id>
                <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
            </pluginRepository>
    </pluginRepositories>
    
    0 讨论(0)
提交回复
热议问题