代码之家  ›  专栏  ›  技术社区  ›  user23381600

如何将org.eclipse.swt打包到项目jar中

  •  0
  • user23381600  · 技术社区  · 7 月前

    我创建了一个Maven项目,其中我使用SWT作为程序的UI。 但是当我打包项目时,出现了一个错误,这阻止了我将引用的其他jar文件打包到我的项目jar中。

    错误消息: [警告]org.eclipse.platform:org.eclipse.swt:jar:3.127.0的POM无效,传递依赖项(如果有的话)将不可用:在为org.eclipse.proplatform:org.eclipse.swt:3.127.0构建有效模型时遇到1个问题 [错误]org.eclipse.platform:org.eclipse.swt的“依赖关系.依赖关系.artifactId”${osgi.platform}:jar,值为'org.eclipse.swt${osgi.platform}'与有效的id模式不匹配@

    原始依赖关系

    <dependency>
        <groupId>org.eclipse.platform</groupId>
        <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
        <version>3.127.0</version>
    </dependency>
    

    我引用了这篇文章( https://www.vogella.com/tutorials/SWT/article.html#exercise-using-the-swt-library-in-a-maven-project )

    在项目中创建一个lib文件夹,放入jar,然后设置依赖关系

    <dependency>
        <groupId>org.eclipse.platform</groupId>
        <artifactId>org.eclipse.swt</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/swt.jar</systemPath>
    </dependency>
    

    我对systemPath使用绝对路径。

    但在包装时,无法将swt装入罐中。

    在插件中,我还设置了maven组装插件。

    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.4.2</version>
      <configuration>
        <archive>
            <manifest>
                <mainClass>cc.demo.SwtDemo.App</mainClass>
            </manifest>
        </archive>
       </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.7.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <mainClass>cc.demo.SwtDemo.App</mainClass>
        </archive>
      </configuration>
    </plugin>
    

    当我运行输出jar时,我会遇到异常。

    线程“main”java.lang.NoClassDefFound中出现异常错误:org/eclipse/swt/widgets/Layout 在cc.derick。YouTube会员表情符号下载器。App.main(App.java:11) 原因:java.lang.ClassNotFoundException:org.eclipse.swt.widgets。布局 在java.base/jdk.internal.loader上。内置类加载器.loadClass(内置类加载器.java:641) 在java.base/jdk.internal.loader上。ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) 位于java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) …1更多

    我尝试在Eclispe中使用Maven Build工具 我还尝试在终端上发出mvn clean package命令。 结果是一样的。

    1 回复  |  直到 7 月前
        1
  •  1
  •   life888888    7 月前
    • Swt根据不同的操作系统需要不同的软件包。

    • 使用不同的配置文件设置与依赖项的groupId、artifactId和version对应的变量swt.groupId、swt.artifactId以及swt.version。

    • 或者,您可以将当前正在开发的操作系统平台对应的swt-artifactId和swt版本硬编码为依赖关系。

    maven pom.xml

    ...
    
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.4.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>cc.demo.SwtDemo.App</mainClass>
          </manifest>
          <manifestEntries>
            <built-by />
            <!-- add empty for not add user name -->
            <Class-Path>.</Class-Path>
            <App-Version>${project.version}</App-Version>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
    
    
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>appassembler-maven-plugin</artifactId>
      <version>2.1.0</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>assemble</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <programs>
          <program>
            <mainClass>cc.demo.SwtDemo.App</mainClass>
            <id>SwtDemo.App</id>
          </program>
        </programs>
        <extraJvmArguments>
    -Duser.language=en
    -Duser.country=US
    -Dfile.encoding=UTF-8
    -Xms128M
    -Xmx756M
        </extraJvmArguments>
        <repositoryLayout>flat</repositoryLayout>
        <useWildcardClassPath>true</useWildcardClassPath>
        <binFileExtensions>
          <!--<unix>.sh</unix> -->
        </binFileExtensions>
        <repositoryName>lib</repositoryName>
      </configuration>
    </plugin>
    
    ...
    
    <dependencies>
      <dependency>
        <groupId>${swt.groupId}</groupId>
        <artifactId>${swt.artifactId}</artifactId>
        <version>${swt.version}</version>
      </dependency>
    </dependencies>
    
    <profiles>
    
      <!-- Linux -->
      <profile>
        <id>linux</id>
        <activation>
          <os>
            <name>Linux</name>
            <arch>amd64</arch>
          </os>
        </activation>
      <properties>
        <swt.groupId>org.eclipse.platform</swt.groupId>
        <swt.artifactId>org.eclipse.swt.gtk.linux.x86_64</swt.artifactId>
        <swt.version>3.119.0</swt.version>
      </properties>
      </profile>
    
      <!-- Windows -->
      <profile>  
        <id>windows</id>
        <activation>
          <os>
            <name>Windows 10</name>
            <arch>amd64</arch>
          </os>
        </activation>
        <properties>
          <swt.groupId>org.eclipse.platform</swt.groupId>
          <swt.artifactId>org.eclipse.swt.win32.win32.x86_64</swt.artifactId>
          <swt.version>3.119.0</swt.version>
        </properties>
      </profile>
      
      <!-- MacOSX -->
      <profile>
        <id>macosx</id>
        <activation>
          <os>
            <name>Mac OS X</name>
            <arch>x86_64</arch>
          </os>
        </activation>
        <properties>
          <swt.groupId>org.eclipse.platform</swt.groupId>
          <swt.artifactId>org.eclipse.swt.cocoa.macosx.x86_64</swt.artifactId>
          <swt.version>3.119.0</swt.version>
        </properties>
      </profile>
    </profiles>
    
    <repositories>
      <!-- add for swt -->
      <repository>
        <id>repo1</id>
        <name>repo1</name>
        <url>https://repo1.maven.org/maven2</url>
      </repository>
    </repositories>