Problem integrating web3j into maven clean install.

huangapple go评论57阅读模式
英文:

Problem integrating web3j into maven clean install

问题

我有一个多模块的Maven项目,我想从.sol文件中生成Java包装器,为了实现这个目标,我正在使用web3j的Maven插件。以下是相关的POM文件部分:

pom.xml:

<build>
	<plugins>
		<plugin>
			<groupId>org.web3j</groupId>
			<artifactId>web3j-maven-plugin</artifactId>
			<version>4.9.4</version>
			<executions>
				<execution>
					<goals>
						<goal>generate-sources</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

pom.xml:

<build>
	<plugins>
		<plugin>
			<groupId>org.web3j</groupId>
			<artifactId>web3j-maven-plugin</artifactId>
			<executions>
				<execution>
					<goals>
						<goal>generate-sources</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<soliditySourceFiles>
					<directory>${project.basedir}/src/main/resources/solidity</directory>
					<includes>
						<include>**/*.sol</include>
					</includes>
				</soliditySourceFiles>
				<packageName>org.example.project-name.wrappers</packageName>
				<outputDirectory>
					<java>${project.build.directory}/generated-sources/web3j/java</java>
				</outputDirectory>
			</configuration>
		</plugin>
	</plugins>
</build>

我使用以下命令使用Maven构建项目:call mvn clean -U install
文件确实生成,并且在正确的位置,但当Maven开始执行install的编译阶段时,会遇到以下错误:

Compilation failure
[ERROR] /C:/source/project/project-name/src/main/java/org/example/child/TestFile.java:[4,51] 
package org.example.project-name.wrappers does not exist

TestFile是一个尝试导入生成的文件之一。

我还有一个openapi代码生成插件,它可以正确生成文件,导入这些文件时没有任何问题。
我没有找到web3j插件中可能遗漏的任何配置选项,也没有找到任何帮助mvn installmvn compile考虑包装器生成目录的方法。

我尝试手动提取install命令的捆绑调用,并手动插入web3j:generate-sources:

 call mvn clean
 call mvn web3j:generate-sources
 call mvn process-resources
 call mvn compile
 call mvn process-test-resources
 call mvn test
 call mvn package
 call mvn install
 call mvn deploy

但这也在编译阶段失败。我假设web3j插件不会更新存储所有生成源的变量,但这只是一个猜测,我不知道该如何修复这个问题。

英文:

I have a multi-module maven project where I want to generate java wrappers from .sol files, to achieve this I'm using web3j's maven plugin. Here are the (relevant sections of the) poms:

main pom.xml:

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.web3j&lt;/groupId&gt;
			&lt;artifactId&gt;web3j-maven-plugin&lt;/artifactId&gt;
			&lt;version&gt;4.9.4&lt;/version&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;goals&gt;
						&lt;goal&gt;generate-sources&lt;/goal&gt;
					&lt;/goals&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

child pom.xml:

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.web3j&lt;/groupId&gt;
			&lt;artifactId&gt;web3j-maven-plugin&lt;/artifactId&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;goals&gt;
						&lt;goal&gt;generate-sources&lt;/goal&gt;
					&lt;/goals&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
			&lt;configuration&gt;
				&lt;soliditySourceFiles&gt;
					&lt;directory&gt;${project.basedir}/src/main/resources/solidity&lt;/directory&gt;
					&lt;includes&gt;
						&lt;include&gt;**/*.sol&lt;/include&gt;
					&lt;/includes&gt;
				&lt;/soliditySourceFiles&gt;
				&lt;packageName&gt;org.example.project-name.wrappers&lt;/packageName&gt;
				&lt;outputDirectory&gt;
					&lt;java&gt;${project.build.directory}/generated-sources/web3j/java&lt;/java&gt;
				&lt;/outputDirectory&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

I'm build the project with maven with the following command: call mvn clean -U install.
The files do generate, and in the correct location, but when maven begins the compile phase of install it runs into the following error:

Compilation failure
[ERROR] /C:/source/project/project-name/src/main/java/org/example/child/TestFile.java:[4,51] 
package org.example.project-name.wrappers does not exist

TestFile is an empty file that tries to import one of the generated files.

I also have an openapi code generator plugin, it does generate files properly, and I run into no issues when importing those.
I didn't find any configuration options in the web3j plugin that I missed and I also didn't find any way to help mvn install or mvn compile consider the directory in which the wrappers are generated.

I tried manually extracting the bundled calls that install makes and manually interjecting the web3j:generate-sources:

 call mvn clean
 call mvn web3j:generate-sources
 call mvn process-resources
 call mvn compile
 call mvn process-test-resources
 call mvn test
 call mvn package
 call mvn install
 call mvn deploy

But this too fails at compile. I'm assuming that the web3j plugin doesn't update a variable storing all generated sources, but that's just a guess and I don't know how I would fix that.

答案1

得分: 0

I managed to solve the issue using org.codehaus:build-helper-maven-plugin.

I added the plugin to the dependencies of my main pom and the relevant child (I use dependency management), then added the plugin to the child's pom:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/web3j</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

I also figured out that my assumption was correct: web3j's plugin does not update the project's sources directory.

英文:

I managed to solve the issue using org.codehaus:build-helper-maven-plugin.

I added the plugin to the dependencies of my main pom and the relevant child (I use dependency management), then added the plugin to the child's pom:

&lt;plugin&gt;
	&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
	&lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;id&gt;add-source&lt;/id&gt;
			&lt;phase&gt;generate-sources&lt;/phase&gt;
			&lt;goals&gt;
				&lt;goal&gt;add-source&lt;/goal&gt;
			&lt;/goals&gt;
			&lt;configuration&gt;
				&lt;sources&gt;
					&lt;source&gt;${project.build.directory}/generated-sources/web3j&lt;/source&gt;
				&lt;/sources&gt;
			&lt;/configuration&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
&lt;/plugin&gt;

I also figured out that my assumption was correct: web3j's plugin does not update the project's sources directory.

huangapple
  • 本文由 发表于 2023年2月6日 19:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定