Javac - plugin not found - java

I am trying to make my own Javac plugin but things aren't going so well already :( I am following this tutorial: https://www.baeldung.com/java-build-compiler-plugin and when I want to test the first basic plugin it can't find it.
When I enter the following command:
javac -cp ./target/classes/javacplugin/ -Xplugin:Getter ./src/main/java/javacPlugin/App.java
I get this error:
plug-in not found: Getter
How do I solve this issue?
This is my sourcetree:
This is my code for the plugin:
package javacplugin;
import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Log;
/**
* JavacPlugin
*/
public class JavacPlugin implements Plugin {
#Override
public String getName() {
return "Getter";
}
#Override
public void init(JavacTask task, String... arg1) {
Context context = ((BasicJavacTask) task).getContext();
Log.instance(context).printRawLines(Log.WriterKind.NOTICE, "Hello from " + getName());
}
}
This is what's inside of com.sun.source.util.Plugin:
javacplugin.JavacPlugin
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>javacplugin</groupId>
<artifactId>javacplugin</artifactId>
<version>1</version>
<name>javacplugin</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>-Xplugin:Getter</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

You likely need your META-INF/ in the root directory, not in resources.
"To achieve this, we need to create a file named com.sun.source.util.Plugin with content which is our plugin’s fully qualified class name (com.baeldung.javac.SampleJavacPlugin) and place it in the META-INF/services directory."
From the referenced baeldung article

I have solved it:
javac -cp ./target/classes -Xplugin:Getter ./src/main/java/javacPlugin/App.java
Is the right command, without /javacplugin.

Related

not getting jre system library in maven project in eclipse java

Not getting java support after creating maven project in eclipse
when maven project created I am not getting src/main/java,src/test/java and jre system libraries folders. i am following steps to create new project
steps to create
** maven project: File-->new-->project-->select maven project-->next-->select create a simple project-->next-->give project id and artifact id-->finish.**
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenProject1</groupId>
<artifactId>MavenProject1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MavenProject1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Yes, you wont get that. Its just a basic, very simple maven project that eclipse creates for you, so that you can structure the project as you like.
You now have 2 options:
create all the structuring by yourself
Create a new maven project and instead of selecting select create a simple project, select from archetype, and from there you can you can select what you like (Ex: maven-archetype-quickstart ) according to what you like.
For more info on setting up maven project with arch type search in youtube. You will get a lot of video tutorials.

Caused by: java.io.IOException: Unable to open root Jar file 'file:/.../Sample/my-app/target/lib/spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar'

I have created a simple SpringBoot project using maven and my only class present is App.java containing
package com.mycompany.app;
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#RestController
public class App {
// Logger logger = LoggerFactory.getLogger(App.class);
#GetMapping("/test-docker")
public String getData() {
return "in docker tutorial project of example";
}
public static void main( String[] args )
{
// System.out.println("SOP method");
SpringApplication.run(App.class, args);
}
}
This is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<build>
<!-- <pluginManagement>lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- </pluginManagement> -->
</build>
</project>
When I do a mvn install, the jar file is created in my target directory. When I run the following command,
java -jar target/my-app-1.0-SNAPSHOT.jar
I get the error:
lrd72218673:my-app subnara$ java -jar target/my-app-1.0-SNAPSHOT.jar
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: Unable to load factories from location [META-INF/spring.factories]
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:151)
at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:122)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:428)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:420)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.mycompany.app.App.main(App.java:24)
... 8 more
Caused by: java.io.IOException: Unable to open root Jar file 'file:/Users/subnara/Documents/Code/DevOps/MicroServices+Docker+Kubernetes/Sample/my-app/target/lib/spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar'
at org.springframework.boot.loader.jar.Handler.getRootJarFile(Handler.java:344)
at org.springframework.boot.loader.jar.Handler.getRootJarFileFromUrl(Handler.java:325)
at org.springframework.boot.loader.jar.Handler.openConnection(Handler.java:99)
at java.base/java.net.URL.openConnection(URL.java:1126)
at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:170)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:133)
at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:122)
at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:139)
... 16 more
Caused by: java.lang.IllegalArgumentException: File /Users/subnara/Documents/Code/DevOps/MicroServices Docker Kubernetes/Sample/my-app/target/lib/spring-boot-actuator-autoconfigure-2.1.0.RELEASE.jar must exist
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.openIfNecessary(RandomAccessDataFile.java:234)
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.<init>(RandomAccessDataFile.java:216)
at org.springframework.boot.loader.data.RandomAccessDataFile$FileAccess.<init>(RandomAccessDataFile.java:206)
at org.springframework.boot.loader.data.RandomAccessDataFile.<init>(RandomAccessDataFile.java:49)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:88)
at org.springframework.boot.loader.jar.Handler.getRootJarFile(Handler.java:338)
... 23 more
I am a noob in this area and any help is much appreciated.
TL;DR: Use the spring-boot-maven-plugin maven plugin and take a look at https://start.spring.io/.
When I try to reproduce the problem:
create a folder with the specified pom.xml and App.java file in it
run mvn install in the folder
run java -jar target/my-app-1.0-SNAPSHOT.jar
I don't run into the problem.
Since we are using the same code (assuming your question contains the same code as you were using), I wonder whether the problem might be related to your version of Maven and Java. I am using Maven 3.6.3 and java 11.0.11.
That being said, I noticed the build configuration in your pom is rather complex. It can be simplified significantly using spring-boot-maven-plugin.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Since your issue seems to be related to missing libraries and this spring-boot plugin also handles adding these libraries to the generated jar, switching to this plugin might also solve your problems.
Since you mention you just created the project, I would like to suggest you take a look at https://start.spring.io/. It's a too to bootstrap new spring boot projects. One advantage, and the reason I'd suggest it to you, is that it generates a project that should work. You don't have to figure out which plugins or dependencies to add, the generator does that for you.

Java Firebase server application.. works fine when I run it in IntelliJ but the .class file doesn't run

I wrote a little server application to run some commands on my firebase Firestore. When I run the project within IntelliJ it works like a charm. So I want to send the .class files to my server now to run it on there but for some reason I can't run it, neither on the machine I am programming on nor the server.
"java MyFirebaseServer" in console gives me following error:
Error: Unable to initialize main class MyFirebaseServer
Caused by: java.lang.NoClassDefFoundError: com/google/auth/oauth2/GoogleCredentials
I used maven for this project and iam not familiar with that, maybe I just don't get that part.
Here is the code of the ServerClass, I stripped it down to minimum, just enough so the error is reproduced:
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.firebase.FirebaseOptions;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class MyFirebaseServer {
public static void main(String[] args) {
System.out.println("HelloWorld");
FileInputStream serviceAccount =
null;
try {
serviceAccount = new FileInputStream("/Users/myserviceaccountfile.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
FirebaseOptions options;
options = null;
try {
options = new FirebaseOptions.Builder()
.setCredentials(ServiceAccountCredentials.fromStream(serviceAccount))
.setDatabaseUrl("here goes my db url")
.build();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now comes the pom , it probably is a total mess as I have not much knowledge about maven:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>MyFirebaseServer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MyFirebaseServer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.11.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-firestore</artifactId>
<version>1.32.0</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-server-sdk</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>MyFirebaseServer</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Did anybody have similar issues or any idea where I go wrong?
Thanks in advance

Maven hangs when taking input from Scanner

I am designing a program that has to run with the mvn test command and take a user input from the command line. When I run the program with mvn test everything works until Scanner.next() is executed, then the CLI hangs and I have to close the program.
my test method
public class AppTest
{
#Test
public void shouldAnswerWithTrue()
{
Scanner sc = new Scanner(System.in);
System.out.println("Awaiting input");
String in = sc.next();
System.out.println("TEST!" + in);
assertTrue( true );
}
}
my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.vogella.build.maven.intro.Main</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Is it possible to handle the input this way with the Scanner class? Or at all through the maven command line interface in this way?
As far as I know you can't use Scanner as part of any code via Maven. Maven is to help you build, test, deploy - not actually help you at runtime. Furthermore, typically speaking, a unit test shouldn't rely on input at runtime.
Solution 1: Run the jUnit test directly with Java from command line
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore AppTest
More information here.
Solution 2: Use system properties
You can then use System.getProperty("myVariable");
which you would run as mvn -Dtest=shouldAnswerWithTrue -DargLine="-myVariable=abc"
See more information about this method here.

Starting with Visual Studio Code for Java with Maven

I'm coming back to my origin of Java development. I'm use to work in C# with Nuget packages. I have basic notion of Maven. I would like to use VSC (Visual Studio Code).
I'm looking how I can import my first package to use a function/method ByteUtils. It seems this method come from an Apache package.
So here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testGroupId</groupId>
<artifactId>artifactId</artifactId>
<version>1.0</version>
<name>artifactId</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
And a screenshot with the issue:
The container 'Maven Dependencies' references non existing library 'C:\Users\xxxx.m2\repository\org\apache\apache\21\apache-21.jar'
Missing artifact org.apache:apache:jar:21
To be complete here is my app.java
package testGroupId;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class App
{
public static void main(String[] args) throws Exception
{
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e)
{
System.out.println(e.getMessage());
}
//byte[] theDigest = md.digest(ByteUtils.intToBytesBigEndian(123456789))
System.out.println("Hello Java");
}
}
My question. Why ny org.apache dependency cannot be loaded or downloaded?
The artifact org.apache:apache:pom:21 is not a JAR artifact instead it is a POM artifact which can be used as a parent for you maven module or can be used in the depencyManagement section.
If you add a POM artifact as normal dependency maven will try to download the jar file for the artifact but it doesn't exist thus the error.
To add as a parent
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
</parent>
To add it in the dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>21</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
If you want to org.apache.commons.compress.utils.ByteUtils then you have to add the following dependency.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>

Categories