Java Documentation from Vscode - java

Hey I am using the Spring tool suite for Vscode It's working great but there is one thing I am missing like in other IDE's you can view the documentation for a specific Annotation or method.
for example:
If I try to view the documentation for any annotation in intellij it shows the documentation properly, but if i try to do the same on vscode it shows up like this:

You can enable the setting java.maven.downloadSources.
Then each time you opened a class file, if the source jar does not exist locally, the extension will trigger a download task for that source jar. After that, next time you open that class file, you can see the source.

This message says that VS Code couldn't find the JAR archive that contains source files for this class. These JARs usually are called XYZ-sources.jar. You need to download an archive like that manually or use the Maven plugin that can download it for you.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>src-dependencies</id>
<phase>package</phase>
<goals>
<goal>sources</goal>
</goals>
<configuration>
<silent>true</silent>
</configuration>
</execution>
</executions>
</plugin>
Then, if you have downloaded JAR archive you need to press Right Mouse Button -> Attach Source.

Related

Visual Studio Code doesn't follow maven goal

I'm working with a Java project and Visual Studio Code as a IDE. This project make use of ActiveJDBC. ActiveJDBC requires instrumentation of class files after they are compiled. Usually this is done with a maven plugin that start in the process-classes phase.
<plugin>
<groupId>org.javalite</groupId>
<artifactId>activejdbc-instrumentation</artifactId>
<version>${activejdbc.version}</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
My workflow with VSC is simply to change the source code and run/debug/test the project. As far as I know VSC compile the java files every time I save, but doesn't follow the maven rules and so, it didn't run the instrumentation process.
I would like to press F5 (debug the project) without having to remeber to call the instrumentation if a modify some class of the model. Any suggestion?

How to create an additional JAR for a specific folder with maven

I have a maven project, I need to generate three separate jars
one for the main application (default)
one for the source code (maven-source-plugin)
and one for my app documentation which reside in target/docs
Question: How can I create a jar containing only the target/docs folder?
You could add the following to your maven pom.xml file, build/plugins section:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<configuration>
<classifier>docs</classifier>
<classesDirectory>${project.build.directory}/docs</classesDirectory>
<includes>**/*</includes>
</configuration>
<id>pack-docs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Which is basically:
Using the maven-jar-plugin, jar goal to pack a new artefact for the project
Adding (or attaching) it as a classifier (docs suffix appended to the normal convention for project artefacts, e.g. artifactId-version.jar)
Setting the target\docs as source of files, using the standard ${project.build.directory} property instead of target (which is its value by default)
Running
mvn clean package
You would then have as part of the output
The jar file normally created, e.g. sample-project-1.0.0-SNAPSHOT.jar
The new docs file, e.g. sample-project-1.0.0-SNAPSHOT-docs.jar

Maven: Replace token in source file before compilation

I want to replace a token #NAME# in a source file (in my case *.java) before compilation.
I try to use google replacer plugin but I am open for anything which will help me.
1.pom.xml
The pom file look like this
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>src/main/java/com/test/sample/File.java</include>
</includes>
<replacements>
<replacement>
<token>#NAME#</token>
<value>New content</value>
</replacement>
</replacements>
</configuration>
</plugin>
But after I run mvn package the output is:
--- replacer:1.5.3:replace (default) # MyProject --- [INFO] Replacement run on 0 file.
Because there is no error I do not know what I have done wrong.
Maybe:
Defined phase is wrong
Defined include is wrong
...
Greetings!
I think there are two options.
If you keep using the plugin I think you need to add the ${basedir} to the include statement:
<include>${basedir}/src/main/java/com/test/sample/File.java</include>
If you dont want to modify the file in src/main but filter the file and add that one to the build you can use the standard resource filtering and the buildhelper plugin to add those "generated sources" to the build.
So step one would be using resource filtering to copy the file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
And then use the http://www.mojohaus.org/build-helper-maven-plugin/ to add those sources to the build.
Some IDEs (IntelliJ) will recognize /target/genereated-sources automatically if you keep using that folder (its not standard but very common). If you search for "maven" and "generated-sources" you will find quite some tutorials.
Hope this helps :)
While this is something you usually should not do in the first place, sometimes you have no choice (in my case it was "converting" an old project to Maven with changing as little of the code as possible). The above somehow did not work (while I could replace a placeholder in the source file and add the generated-sources folder to be compiled, it complained about duplicate source files).
Then I found an easier way by using the templating-maven-plugin as described here http://www.mojohaus.org/templating-maven-plugin/examples/source-filtering.html:
Put the file with the placeholder in the folder /src/main/java-templates. Excerpt from my source code:
public static final String APPLICATION_VERSION = "r${project.version}";
Add the following to your pom's plugins section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>filter-src</id>
<goals>
<goal>filter-sources</goal>
</goals>
</execution>
</executions>
</plugin>

build-helper-maven-plugin adding additional source

I am trying to add an additional source folder to my current maven project by using build-helper-maven plugin.
This source folder contains some common classes, like utility classes.
For that, here is my relevant pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>C:/Users/CommonIncludes/src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Eclipse is showing the following error :
Build path entry is missing.
Project->Right Click->Java build path->Source->
Project/Users/CommonIncludes/src(missing)
Here the additional source location : "C:/Users/CommonIncludes/src" is outside of the workspace of the current project. But eclipse always treating this as a location from current project.
I am using Eclipse 4.3 and m2e.
How can I overcome this error through MAVEN, so that Eclipse can identify the linked source from correct location.? Or is there any alternate way to do this using MAVEN?
Any help will be greatly appreciated. Thanks in advance.
Found it in an alternate way..works great.!
Steps include
1. Removed build-helper-maven-plugin from pom .
2. Created another maven project and added the common classes in it. Added maven-source-plugin in this pom to generate sources.
3. To the same pom, added maven-dependency-plugin to copy this generated sources to the desired location (My project's src/main/java).
4. Run a maven build for common classes project.
Now the common code in my project. Thanks.
I had a lot of inconsistent trouble with this plugin.
by far the simplest workaround in my case was simply to apply the addition via the standard 'resources' options.
hope this helps someone.
<build>
<resources>
...
<resource>
<directory>${project.build.directory}/generated-sources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
</resource>
</build>

Packaging war file with maven

I thought that, if my project is named my-web-application, than when I call mvn clean install maven first compiles to target\my-web-application-1.0 folder and than "zips" that folder to my-web-application-1.0.war...
But as I found recently it's not how it (maybe just with default settings) works.
I'm using some 3rd party components and I'd like to modify some styles (.css) and behavior (.js) for those components, so I simply replace content in dependency jar with my modification using
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<plugin>
execution example follows (it zips org folder to componentToModify.jar)
<execution>
<id>fix</id>
<phase>package</phase>
<configuration>
<executable>jar</executable>
<workingDirectory>${basedir}</workingDirectory>
<arguments>
<argument>-uvf</argument>
<argument>target/${project.artifactId}-${project.version}/WEB-INF/lib/componentToModify.jar</argument>
<argument>org</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
what correctly modifies the componentToModify.jar in the target\my-web-application-1.0, but final war doesn't contain the modifications. It seems that libs are zipped to war from local repository.
Is there some reason for such behavior and can I change it somehow?
Not completely sure but would using an Overlay help here?
http://maven.apache.org/plugins/maven-war-plugin/overlays.html
If all else fails, you may just want to modify the 3rd party application and produce your own jar with your changes.

Categories

Resources