Eclipse project does not recognize Swagger Codegen artefacts - java

I added Swagger Codegen to my Eclipse project by modifying my pom.xml file directly:
<plugin>
<!--
Plugin that provides API-first development using swagger-codegen to
generate Spring-MVC endpoint stubs at compile time from a swagger definition file
-->
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${swagger-codegen-maven-plugin.version}</version>
<executions>
<execution>
<id>generate-swagger-javaclient</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/swagger/remote-api.json</inputSpec>
<language>java</language>
<apiPackage>com.myproj</apiPackage>
<modelPackage>com.myproj.model</modelPackage>
<generateSupportingFiles>true</generateSupportingFiles>
<generateApiTests>false</generateApiTests>
<configOptions>
<dateLibrary>java8</dateLibrary>
</configOptions>
<library>resttemplate</library>
</configuration>
</execution>
</executions>
</plugin>
If I run Maven update or the Maven generate-sources target, I get all the artefacts generated in my Eclipse project's /target/generated-sources/swagger/src folder.
However, Eclipse does not recognize them. Am I supposed to edit my Eclipse build path manually like some commoner, or is Eclipse supposed to recognize this new source folder automatically?

In Eclipse 4.9.0 (2018-09) I had to add the generated-sources folder as a source folder for the project like this:
open the "Navigator" view
browse to target/generated-sources (or whatever your folder for the generated sources is).
right-click on that folder
click "Build Path" -> "Use as Source Folder"
Menu Tree Screenshot
Does not solve the "Plugin execution not covered by lifecycle configuration" issue.

You can use build-helper-maven-plugin to add generated sources to you build path, if this plugin itself does not support it.
<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.basedir}/target/generated-sources/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

The answer, at this time, is that adding the generated source folder to the build path seems to be mandatory.

Ctrl+shift+R
Opens 'Open Resource' dialog box.
Besides the message line 'Enter Resource name ....' there is a drop down option.
When u click it, u get options as 'show status' , 'show derived sources' etc.
You must ensure that 'show derived sources' is checked.
Eclipse will start showing swagger generated artifacts as well.

Related

why is 'dependency:copy-dependencies' executed unexpectedly during tycho build?

I want to add JARs to a plugin. Previously, I added them by hand but thought it would be a good idea to add them via dependency definitions.
I created a pom.xml in the bundle and added an execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
As soon as I save the file the dependency JARs are all copied to the lib folder. So far so good...
When I start a tycho build, such as mvn install, the build succeeds but the lib folder now contains a new folder lib/myplugin-1.0.0.qualifier-lib and this folder contains copies of all the JARs in lib but they all have the extension .jar.jar.
If I remove the copy-dependencies execution before starting the tycho build then this folder is not created.
Can anyone explain why this is happening and what I should do to avoid this behaviour?

Why doesn't Eclipse m2e automatically add generated source folder to classpath?

I'm using 2018-12 with Java 8, but I'm running Eclipse itself with Java 11.
I have version "1.10.0.20181127-2120" of the m2e plugin.
I have a bunch of projects with similar characteristics. Maven projects run with Spring Boot. They are all stored in git.
For some reason, one of those projects was somewhat brain-damaged this morning. It still would build fine from the command line. Eclipse still knew it was a maven project, but it had no source folders. The .classpath file wasn't empty, it was just very minimal.
I got this particular problem resolved by simply copying the .classpath file from a similar project into this project. That fixed most of the problems, but I remembered that this one project uses a code generator plugin along with the "build-helper-maven-plugin" to indicate where the source is being generated. I found that Eclipse didn't have the generated sources folder as a source folder. I thought adding the "build-helper-maven-plugin" reference was what I needed to do to make Eclipse M2E to automatically detect this, but it doesn't appear to be working.
Once I manually added the generated sources folder as a source folder, that cleaned out the last compile errors.
This is a hopefully relevant excerpt from my pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/java-gen</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.34</version>
<configuration>
<sourcePaths>
<sourcePath>${basedir}/src/main/schema/...</sourcePath>
<sourcePath>${basedir}/src/main/schema/...</sourcePath>
</sourcePaths>
<targetPackage>...</targetPackage>
<annotationStyle>jackson2</annotationStyle>
<useCommonsLang3>true</useCommonsLang3>
<useDoubleNumbers>true</useDoubleNumbers>
<includeAccessors>true</includeAccessors>
<includeAdditionalProperties>true</includeAdditionalProperties>
<sourceType>jsonschema</sourceType>
<generateBuilders>true</generateBuilders>
<includeJsr303Annotations>true</includeJsr303Annotations>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Update:
Deleting the Eclipse project and reimporting from git made no difference. Same result.

How to add generated sources of dependent project to build path?

I'm using Intellij and a coworker is using Eclipse. There is a datamodel project that most components depend on that has all the JPA code.
One of datamodel dependencies is utils. In utils there are generated sources. My coworker in Eclipse, adds the target/generated-sources of utils to the build path and everything builds and runs fine within Eclipse.
In Intellij, when I go to Project Structure, do I need to go to utils and add the target/generated-sources of utils as a Source folder to be equivalent?
Or do I need to add that module as a dependency?
Edit:
In utils pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="target/generated-sources" />
<exec executable="protoc">
<arg value="--java_out=target/generated-sources" />
<arg value="src/main/resources/utilities.proto" />
</exec>
</tasks>
<sourceRoot>target/generated-sources</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
You could use utils which were built by your coworker as a dependency.
But if you ever want to change sources of utils then you should fix its pom.xml by adding:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>test</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
IntelliJ supports the plugin and the generated-sources folder will be marked as Source folder after clicking on Reimport.
That depends if the project you build locally actually generates the sources. One instance Intellij picked up the generated sources automatically for me. If Intellij does not do it automatically then you should it as a source folder manually.
If your project doesn't generate the sources then you should add them as a dependency.
So in your case the decision is already made in the utils dependency/project. Does the utils dependency/project generate the sources or does it contain the sources?

Scala Maven Plugin not handling Scala source folder and reporting lifecycle error

I am trying to import a java+Scala project into Eclipse(v4.6.1 Neon). This consist a parent project with scala-maven plugin configured as follows:
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
While importing the project it show an error:
No marketplace entries found to handle scala-maven-plugin:3.2.1:add-source in Eclipse. Please see Help for more information.
While imported to eclipse, there is no source folder created in eclipse for scala(like for java src/main/java and src/main/resources). Also in child projects, it shows this Error.
Plugin execution not covered by lifecycle configuration: net.alchim31.maven:scala-maven-plugin:3.2.2:add-source (execution: scala-compile-first, phase: process-resources).
Although I am able to build the project from command line.
I also have tried installing Scala IDE Plugin for eclipse, but nothing worked. I dont't want to mark this goal as ignored.
The goal add-source, should be used with "mvn eclipse".
But you seem to use m2e/m2eclipse, in this case did you install https://github.com/sonatype/m2eclipse-scala. this plugin setup your eclipse project to use scala + setup of scala folder.
From my personal experience, it's easier to mixe .java and .scala into the same source folder. Easier for IDE, build tools and dev (less directories to navigate into).
Hey PyThon read my answer here . It has the exact pom.xml that you need. It works great.

Maven plugin - edit files in target (war)

I am developing some little maven plugin and I need to edit some css and js files from target (not from src!). And I can't understand on what phase I can do it.
To get access to src I use the phases:generate-resources and the following code:
MavenProject project = (MavenProject) getPluginContext().get("project");
String projectDir=project.getBasedir().toString();
How can I get target when all js,css files are copied there but war file is not generated in order to edit some files from target and get final war with some modifications of js and css files?
EDIT
What for I need it. I have js files in my project: a.js, b.js. I want to obfuscate them via maven. I mean, obfuscate when I build project. And of course all files in final war must be obfuscated but the same files in src must be left unobfuscated.
Besides, I need to combine some obfuscated files into one file.
I found the answer. The problem is that we must add some logic between "prepare-package" and "package" phases. As we user maven-war-plugin we can do it using exploded goal. From official docs:
Create an exploded webapp in a specified directory.
And here it's necessary to remember one important thing that maven after version 2.0.1 copies resources twice so if we want to use maven 2.5 we must use <useCache>true</useCache>. So final solution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<useCache>true</useCache>
</configuration>
</plugin>
<plugin>
<groupId>my plugin</groupId>
<artifactId>...</artifactId>
<version>....</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>...</goal>
</goals>
</execution>
</executions>
</plugin>

Categories

Resources