본문 바로가기
Linux

[Linux/Ubuntu] maven pom.xml 수정 | Maven build plugin 설정 | 크롬&크롬드라이버 버전 맞추기 |

by dev_haha 2024. 1. 19.

vscode연동 우분투에서 자동완성(탭 키) 안된다면

sudo apt install bash-completion

설치하면 됨!!

 


Maven에서 java를 실행가능한 jar파일(Runnable jar)로 만드는 방법은 3가지.

  1. maven-jar-plugin
    • src/main/java, src/main/resources 만 포함한다.
  2. maven-assembling-plugin
    • dependencies jar 파일들을 함께 모듈화 한다.
  3. maven-shade-plugin
    • dependencies jar 파일을 함께 모듈화 하고 중복되는 클래스가 있을 경우 relocate 한다.

 

https://stackoverflow.com/questions/11947037/what-is-an-uber-jar-file

 

What is an uber JAR file?

I am reading Maven documentation and came across the name uber-jar. What does an uber-jar mean and what are its features/advantages?

stackoverflow.com


pom.xml

pom에 셀레니움 관련 dependency들 넣어주고 jar로 실행시켜도 오류가 났다.

알고보니 pom에 build가 없어서 그렇다고 하셨다.

그래서 build를 아래와 같이 추가하고

 

<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.first.mavenapp</groupId>
  <artifactId>realMaven</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>realMaven</name>
  <url>http://maven.apache.org</url>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.141.59</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.141.59</version>
    </dependency>
  </dependencies>
</project>

mvn clean install 해주고

jar파일을 열어서 확인해보니

오류가 나기 시작했다.

왜 자바 버전 9를 열려고 하니?

 

지피티 한테 물어보니까

특정 버전을 제외하도록 하는 filter를 추가하라길래

<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.first.mavenapp</groupId>
  <artifactId>realMaven</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>realMaven</name>
  <url>http://maven.apache.org</url>
  <build>
    <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/versions/**</exclude>
                    </excludes>
                </filter>
            </filters>
          </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>3.141.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-remote-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>3.141.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>3.141.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>3.141.0</version>
    </dependency>
  </dependencies>
</project>

위처럼 추가해주고

다시 빌드 (mvn clean install)

그랫더니 모든 의존성이 다 잘 담겻다 jar에

겁나 긴 결과가 나옴.

 

그래서 이제 두군두근하며 SeleniumSample 파일을 실행해 보는데...

 

 

에러 전문 ↓

더보기
gkrud@DESKTOP-HJPVGM2:~/mavenTestProject/realMaven/target$ java -cp realMaven-1.0-SNAPSHOT.jar com.first.mavenapp.SeleniumSample 
Starting ChromeDriver 120.0.6099.109 (3419140ab665596f21b385ce136419fde0924272-refs/branch-heads/6099@{#1483}) on port 28762
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: 비밀이얌
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x55bd04b9bf83 <unknown>
#1 0x55bd04854cf7 <unknown>
#2 0x55bd0488c60e <unknown>
#3 0x55bd0488926e <unknown>
#4 0x55bd048d980c <unknown>
#5 0x55bd048cde53 <unknown>
#6 0x55bd04895dd4 <unknown>
#7 0x55bd048971de <unknown>
#8 0x55bd04b60531 <unknown>
#9 0x55bd04b64455 <unknown>
#10 0x55bd04b4cf55 <unknown>
#11 0x55bd04b650ef <unknown>
#12 0x55bd04b3099f <unknown>
#13 0x55bd04b89008 <unknown>
#14 0x55bd04b891d7 <unknown>
#15 0x55bd04b9b124 <unknown>
#16 0x7f45f152cac3 <unknown>

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
        at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
        at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:531)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
        at com.first.mavenapp.SeleniumSample.main(SeleniumSample.java:17)

댕같이 실패!


 

일단 메이븐으로 돌리기 전에 

그냥 Run으로 실행시키는거 성공해보자. 

package com.first.mavenapp;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;


public class SeleniumSample {
    public static final String WEB_DRIVER_ID = "webdriver.chrome.driver";
    // /usr/bin/chromedriver
    public static final String WEB_DRIVER_PATH = "/usr/bin/chromedriver";

    public static void main(String[] args) {
        
        System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH);
        WebDriver driver = new ChromeDriver();

        try {
            driver.get("https://www.google.com");

            WebElement searchBox = driver.findElement(By.name("q"));

            searchBox.sendKeys("Selenium");

            searchBox.submit();

            System.out.println("Page title is : " + driver.getTitle());

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }

}

 

실행했을 때 발생하는 에러는

gkrud@DESKTOP-HJPVGM2:~$  /usr/bin/env /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp /tmp/cp_bwmjhcpkqh92a8ud61ut92iou.jar com.first.mavenapp.SeleniumSample 
Starting ChromeDriver 120.0.6099.109 (3419140ab665596f21b385ce136419fde0924272-refs/branch-heads/6099@{#1483}) on port 28987
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: Chrome failed to start: exited normally.
  (session not created: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:09:30'
System info: host: 'DESKTOP-HJPVGM2', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.16.3-microsoft-standard-WSL2', java.version: '1.8.0_392'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x560fc6261f83 <unknown>
#1 0x560fc5f1acf7 <unknown>
#2 0x560fc5f5260e <unknown>
#3 0x560fc5f4f26e <unknown>
#4 0x560fc5f9f80c <unknown>
#5 0x560fc5f93e53 <unknown>
#6 0x560fc5f5bdd4 <unknown>
#7 0x560fc5f5d1de <unknown>
#8 0x560fc6226531 <unknown>
#9 0x560fc622a455 <unknown>
#10 0x560fc6212f55 <unknown>
#11 0x560fc622b0ef <unknown>
#12 0x560fc61f699f <unknown>
#13 0x560fc624f008 <unknown>
#14 0x560fc624f1d7 <unknown>
#15 0x560fc6261124 <unknown>
#16 0x7fce61e68ac3 <unknown>

        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
        at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
        at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:531)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
        at com.first.mavenapp.SeleniumSample.main(SeleniumSample.java:17)

 

이다.

 

열심히 구글링을 해보던 중 

org.openqa.selenium.SessionNotCreatedException 해결할 때

크롬, 크롬드라이버를 최신 버전으로 하면 된다는 말이 있어서 시도해보겠다. 

https://stackoverflow.com/questions/40067849/org-openqa-selenium-sessionnotcreatedexception-session-not-created-exception

 

org.openqa.selenium.SessionNotCreatedException: session not created exception

I'm trying to run some Selenium tests using the ChromeDriver, and I started getting this error: Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129) on port 42985 Only local

stackoverflow.com

 

근데 크롬 최신 버전은 120.0.6099.224이고

크롬 드라이버는 120.0.6099.109 여서

맞춰주려고

 

일단 크롬과 크롬 드라이버를 삭제하고

sudo apt remove google-chrome-stable
rm google-chrome-stable_current_amd64.deb
rm chromedriver-linux64.zip		// 파일삭제
rm -r chromedriver-linux64		// 디렉토리 삭제

 


크롬 이전 버전 다운로드를 검색하여 109-1를 다운받았다.

 

https://jinseongsoft.tistory.com/430

 

[Linux] Ubuntu 리눅스 Chrome 설치 방법 (command line)

들어가며 리눅스 환경에서 터미널에서 Command Line을 통한 chrome 설치 방법을 알아보겠습니다. 설치 방법 아래 명령어를 참고하시면 됩니다. 현재 기준으로 stable 버전으로 install 하게 됩니다. curl -L

jinseongsoft.tistory.com

 

크롬 이전 버전 확인 참고 ↓

https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable

 

UbuntuUpdates - Package "google-chrome-stable" (stable )

Package "google-chrome-stable" Name: google-chrome-stable Description: The web browser from Google Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Latest version: 120.0.6099

www.ubuntuupdates.org

 

설치 명령어 ↓

curl _LO http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_120.0.6099.109-1_amd64.deb

다운 받고

sudo apt install google-chrome-stable_120.0.6099.109-1_amd64.deb

설치해주면 됨. 

확인 하면

 

쫘란-


크롬드라이버 설치

wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/120.0.6099.109/win64/chromedriver-win64.zip
unzip chromedriver-win64.zip

 

하고 chromedriver.exe를 /usr/bin 으로 옮겨줌.


SessionNotCreatedException 에러

Run 돌리면

에러낭

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn't exist)

 

그래서 ChromeOptions를 추가해보라는 글이 있어서 추가해봄

참고 ↓

https://synkc.tistory.com/entry/Chromedriver-DevToolsActivePort-file-doesnt-exist-%EC%97%90%EB%9F%AC-%ED%95%B4%EA%B2%B0%EB%B2%95

 

Chromedriver DevToolsActivePort file doesn't exist 에러 해결법

간밤에 삽질하게 만들었다. chromedriver가 업데이트 되면서 DevToolsActivePort를 찾을 수 없다는 에러를 뿜게 되었다. chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('--headless')chrome_options.add_argumen

synkc.tistory.com

ChormeOptions 추가

 

 

해도 안됨^^