2017-10-25 8 views
0

Maven dependencyでSQL Server JDBCドライバを含むサンプルjarをビルドしようとしています。 dbに接続してクエリを実行するクラスは、IDE(IntelliJ)でコンパイルして正しく実行します。Maven shadeプラグインを使用してSQL Server JDBCドライバを含むUber/fat jar

IntelliJから「mvnクリーンインストール」または「インストール」を実行すると、作成したjarファイルには、SQL Server JDBC jarファイルは作成されていないdbクラスのみが格納されます(展開形式にする必要があります) 。

<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>com.company</groupId> 
    <artifactId>JavaMavenProject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>JavaMavenProject</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> 
    <dependency> 
     <groupId>com.microsoft.sqlserver</groupId> 
     <artifactId>mssql-jdbc</artifactId> 
     <version>6.3.4.jre8-preview</version> 
     <scope>test</scope> 
    </dependency> 


    </dependencies> 

    <build> 
    <pluginManagement> 
     <plugins> 
     <!--For building an uber jar--> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>3.1.0</version> 
      <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
       <goal>shade</goal> 
       </goals> 
       <configuration> 
       <!--<artifactSet> 
        &lt;!&ndash; groupId:artifactId[[:type]:classifier]&ndash;&gt; 
        <includes> 
        <include>com.microsoft.sqlserver:mssql-jdbc</include> 
        </includes> 
       </artifactSet>--> 

       <filters> 
        <filter> 
        <artifact>*:*</artifact> 
        <excludes> 
         <exclude>META-INF/*.SF</exclude> 
         <exclude>META-INF/*.DSA</exclude> 
         <exclude>META-INF/*.RSA</exclude> 
        </excludes> 
        </filter> 
       </filters> 
      </configuration> 
      </execution> 
      </executions> 
     </plugin> 
     </plugins> 
    </pluginManagement> 

    </build> 

</project> 

興味深い部分は、私は別の依存関係が含まれている場合例えばことです:スカラ座とSQL Serverの前にそれを提供、ユーバージャーは、必要な依存関係の両方が含まれており、SQLコードは、ターゲットジャーからも実行されます。 SQL Serverのjdbc jarが何らかの理由でjarの構築中に選択されないと思う。 POM以下完璧に動作し、所望のユーバージャー与える:

<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.company</groupId> 
    <artifactId>JavaMavenUber</artifactId> 
    <packaging>jar</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>JavaMavenUber</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <scala.version>2.12.3</scala.version> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>org.scala-lang</groupId> 
     <artifactId>scala-library</artifactId> 
     <version>${scala.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>com.microsoft.sqlserver</groupId> 
     <artifactId>mssql-jdbc</artifactId> 
     <version>6.3.4.jre8-preview</version> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 
    <build> 

    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <executions> 
      <execution> 
      <phase>compile</phase> 
      <goals> 
       <goal>compile</goal> 
      </goals> 
      </execution> 
     </executions> 
     </plugin> 
     <!--For building an uber jar--> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>3.1.0</version> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>shade</goal> 
      </goals> 
      <configuration> 
       <artifactSet> 
       <!--groupId:artifactId[[:type]:classifier]--> 
       <includes> 
        <include>org.scala-lang:scala-library</include> 
        <include>com.microsoft.sqlserver:mssql-jdbc</include> 
       </includes> 
       </artifactSet> 
       <filters> 
       <filter> 
        <artifact>*:*</artifact> 
        <excludes> 
        <exclude>META-INF/*.SF</exclude> 
        <exclude>META-INF/*.DSA</exclude> 
        <exclude>META-INF/*.RSA</exclude> 
        </excludes> 
       </filter> 
       </filters> 

      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

編集-1:@Gladiator

<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>com.company</groupId> 
    <artifactId>JavaMavenProject</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>JavaMavenProject</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <maven.compiler.source>1.8</maven.compiler.source> 
    <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 

    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> 
    <dependency> 
     <groupId>com.microsoft.sqlserver</groupId> 
     <artifactId>mssql-jdbc</artifactId> 
     <version>6.3.4.jre8-preview</version> 
    </dependency> 
    </dependencies> 

    <build> 
    <pluginManagement> 
     <plugins> 
     <!--For building an uber jar--> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>3.1.0</version> 
      <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
       <goal>shade</goal> 
       </goals> 
       <configuration> 
       <shadedArtifactAttached>true</shadedArtifactAttached> 
       <shadedClassifierName>all</shadedClassifierName> 

       <filters> 
        <filter> 
        <artifact>*:*</artifact> 
        <excludes> 
         <exclude>META-INF/*.SF</exclude> 
         <exclude>META-INF/*.DSA</exclude> 
         <exclude>META-INF/*.RSA</exclude> 
        </excludes> 
        </filter> 
       </filters> 
       </configuration> 
      </execution> 
      </executions> 
     </plugin> 
     </plugins> 
    </pluginManagement> 

    </build> 

</project> 

答えて

0

をコンパイルするための試験から以下の変更範囲によって示唆されるように修飾されたPOMを
<dependency> 
    <groupId>com.microsoft.sqlserver</groupId> 
    <artifactId>mssql-jdbc</artifactId> 
    <version>6.3.4.jre8-preview</version> 
    <scope>test</scope> 
</dependency> 

以下が役に立ちます。

<plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-shade-plugin</artifactId> 
     <version>2.4.3</version> 
     <executions> 
      <execution> 
       <phase>package</phase> 
       <goals> 
        <goal>shade</goal> 
       </goals> 
       <configuration> 
        <shadedArtifactAttached>true</shadedArtifactAttached> 
        <shadedClassifierName>all</shadedClassifierName> 

       </configuration> 
      </execution> 
     </executions> 
    </plugin> 
+0

jarファイルは変更されていません –

+0

ここに更新されたpomファイルを追加することができます – gladiator

+0

元の投稿にEdit-1が変更されたpomファイルを持っていますか? –

関連する問題