欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven的的误用一例及解决办法

程序员文章站 2022-03-14 18:43:48
...
手上一个工程使用maven来构建,POM.xml中有如下内容:

引用
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>6</version>
<scope>system</scope>
<systemPath>${basedir}/ext_lib/ojdbc6.jar</systemPath>
</dependency>


使用maven 3.3.9来run的时候报错:

引用
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.kingsoft:passport3:jar:1.0.0
[WARNING] 'dependencies.dependency.systemPath' for com.oracle:ojdbc:jar should not point at files within the project directory, ${basedir}/ext_lib/ojdbc6.jar will be unresolvable by dependent projects @ line 212, column 16
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]


引用
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:run (default-cli) on project passport3: An exception occured while running. null: InvocationTargetException: Error creating bean with name 'mybatisConfig': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource com.kingsoft.passport3.config.MybatisConfig.dataSource;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception;
nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active). -> [Help 1]


搜了下,在http://*.com/questions/3642023/having-a-3rd-party-jar-included-in-maven-shaded-jar-without-adding-it-to-local-r找到原因如下:

引用
because the system scoped dependencies are assumed to be always present (this is exactly what the system scope is about) so they won't be included.

system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessary in certain edge cases. The system scope includes the <systemPath> element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.

So, instead of using the system scope, either:

    Add your libraries to your local repository via install:install-file. This is a quick and dirty way to get things working, it might be an option if you're alone but it makes your build non portable.
    Install and run an "enterprise repository" like Nexus, Archiva, or Artifactory and add your libraries via deploy:deploy-file. This is the ideal scenario.
    Setup a file based repository as described in this previous answer and put your libraries in there. This is the best compromise if you don't have a corporate repository but need to work as a team and don't want to sacrifice portability.


最后的解决办法是:

1 改写POM.xml中的dependency为如下内容:

引用
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>6</version>
</dependency>


2 增加如下plugin:

引用
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
      <executions>
        <execution>
          <id>install-external</id>
          <phase>clean</phase>
          <configuration>
            <file>${basedir}/ext_lib/ojdbc6.jar</file>
            <repositoryLayout>default</repositoryLayout>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>6</version>
            <packaging>jar</packaging>
            <generatePom>true</generatePom>
          </configuration>
          <goals>
            <goal>install-file</goal>
          </goals>
        </execution>
      </executions>
    </plugin>


3 运行mvn clean和mvn install

至此问题解决。
相关标签: maven