0
0

More than 1 year has passed since last update.

Partner WSDLを使用したMaven(Java)プロジェクトの作成方法

Last updated at Posted at 2022-08-24

経緯

Javaからsalesforceに接続する記載が少ないため、備忘録として記載。

手順

参考サイト:Java 開発者環境の設定

前提

  1. javaがダウンロードされていること

    参考サイト:Javaダウンロード

  2. Mavenがダウンロードされていること
    • インストール方法
      • Windows: ダウンロードサイト
      • macOS: コマンド
        $ brew install maven
        $ vi ~/.zprofile
        
        .zprofile
        # M2_HOMEを作成
        export M2_HOME=/usr/local/Cellar/maven/3.8.1/libexec/<br>
        # PATHの先頭に追加
        export PATH=$M2_HOME/bin:$PATH
        
  3. 以下のコマンドが起動すること
    $ mvn -v
    Apache Maven 3.8.6 (xxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
    Maven home: /usr/local/Cellar/maven/3.8.6/libexec
    Java version: 18.0.2, vendor: Homebrew, runtime: /usr/local/Cellar/openjdk/18.0.2/libexec/openjdk.jdk/Contents/Home
    Default locale: ja_JP, platform encoding: UTF-8
    OS name: "mac os x", version: "12.5.1", arch: "x86_64", family: "mac"
    

プロジェクトの作成

  1. 以下のコマンドにてプロジェクトを作成
    $ mvn archetype:generate \
        -DarchetypeGroupId=com.wsc \
        -DarchetypeArtifactId=wsc \
        -DarchetypeVersion=1.0
    

ライブラリ(jarファイル)のダウンロード

  1. libs/フォルダをプロジェクト内部に作成
    $ mkdir libs
    
  2. 下記のライブラリをダウンロードしlibs/フォルダに移動

WSDLファイルのダウンロード

  1. 必要なWSDLファイルを取得 ※例としてPartner(パートナーWSDLの生成)を選択
    スクリーンショット 2022-08-23 15.42.19.png
    1. [設定]からクイック検索でAPIを検索インテグレーションのAPIを選択。
    2. ダウンロード対象のリンクを右クリックし、リンク先を別名で保存をクリックして保存。(Macの場合)
    3. ダウンロードしたpartner.wsdllibs/フォルダに移動。
  2. partner.wsdlからpartner.jarの作成
    $ cd libs/
    $ java -classpath force-wsc-56.0.0.jar:ST4-4.3.jar:antlr-runtime-3.5.2.jar com.sforce.ws.tools.wsdlc partner.wsdl partner.jar
    

ライブラリ(jarファイル)の読み込み

partner.jarの読み込み

  1. 以下のmvnのコマンドにて生成したライブラリpartner.jarをプロジェクトに読み込ませる
    mvn install:install-file -Dfile=libs/partner.jar -DgroupId=com.sforce.soap.partner -DartifactId=partner -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
    
  2. 以下をpomファイルの<dependencies>に追記
    pom.xml
    <dependency>
      <groupId>com.sforce.soap.partner</groupId>
      <artifactId>partner</artifactId>
      <version>1.0</version>
    </dependency>
    

force-wsc-XX.X.X.jarの読み込み

  1. 以下のmvnのコマンドにて生成したライブラリforce-wsc-XX.X.X.jarをプロジェクトに読み込ませる
    mvn install:install-file -Dfile=libs/force-wsc-56.0.0.jar -DgroupId=co.ws -DartifactId=ws -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
    
  2. 以下をpomファイルの<dependencies>に追記
    pom.xml
    <dependency>
      <groupId>com.force.api</groupId>
      <artifactId>force-wsc</artifactId>
      <version>56.0.0</version>
    </dependency>
    

実際に動かしてみる

  1. 設定したライブラリをインストール
    $ mvn install
    
  2. プログラムを作成
    App.java
    package wsc;
    
    import com.sforce.soap.partner.Connector;
    import com.sforce.soap.partner.PartnerConnection;
    import com.sforce.ws.ConnectionException;
    import com.sforce.ws.ConnectorConfig;
    
    public class App {
        static final String USERNAME = "*********";
        static final String PASSWORD = "*********";
    
        public static void main(String[] args) {
            printFullName();
        }
    
        public static void printFullName() {
            ConnectorConfig config = new ConnectorConfig();
            config.setUsername(USERNAME);
            config.setPassword(PASSWORD);
    
            try {
                PartnerConnection pc = Connector.newConnection(config);
                String fullName = pc.getUserInfo().getUserFullName();
                System.out.println(fullName);
            } catch (ConnectionException e) {
                e.printStackTrace();
            }
        }
        
    }
    
  3. プログラムをコンパイル
    $ mvn compile
    
  4. プロジェクトをパッケージ化
    $ mvn package
    
  5. プロジェクトを実行(設定)
    pom.xml
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <configuration>
        <mainClass>wsc.App</mainClass>
      </configuration>
    </plugin>
    
  6. プロジェクトを実行
    $ mvn exec:java
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0