- Saved searches
- Use saved searches to filter your results more quickly
- License
- snyk-labs/snyk-java-jar-test
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Test jar file java
- How to create a jar containing test classes
- The easy way
- The preferred way
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Test Java JARs using Snyk
License
snyk-labs/snyk-java-jar-test
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
You will need Python 3.7 and pipenv.
git clone https://github.com/snyk-samples/snyk-java-jar-test.git cd snyk-java-jar-test pipenv install pipenv shell
You will need to authorize your Snyk CLI.
After activating an appropriate Python shell, you can do things like:
Test a single JAR in the local directory:
python snykjar.py gson-2.3.1.jar
Test a multiple specific JARs in the local directory:
python snykjar.py gson-2.3.1.jar commons-collections-3.2.1.jar jackson-core-2.9.8.jar
Test a single JAR in an arbitrary directory:
python snykjar.py /path/to/jars/gson-2.3.1.jar
Test multiple JARs in arbitrary directories:
python snykjar.py /path/to/jars/gson-2.3.1.jar /path/to/jars/commons-collections-3.2.1.jar /some/other/path/to/jars/jackson-core-2.9.8.jar
Test all the JARs in the current directory:
Test a directory full of JARs:
python snykjar.py /path/to/jars
—jsonOutput= — this will save the output in a JSON file which is ideal for parsing.
—orgId — you only need to use this if your default organization in Snyk is not an organization that has API access. In most cases you won’t need to use this. You can see your default Snyk organization by going to Account Settings->Preferred Organization.
—outputPom= — use this if you just want to get a pom.xml generated as output with all the detected Java packages. If you use this option, you the detected packages will not be tested and you will not get JSON output even if you use the —jsonOutput option. You might want to use this option to generate a pom.xml and then either test it with the snyk CLI (ex snyk test —file=pom.xml ) or push the list of detected Java packages into Snyk and test monitor them there using snyk monitor —file=pom.xml —project-name= . For this to work, the filename needs to be pom.xml .
About
Test Java JARs using Snyk
Test jar file java
- Apache /
- Maven /
- Plugins /
- Apache Maven JAR Plugin /
- How to create a jar containing test classes
- | Last Published: 2022-09-12
- Version: 3.3.0
How to create a jar containing test classes
When you want to create a jar containing test-classes, you would probably want to reuse those classes. There are two ways to solve this:
- Create an attached jar with the test-classes from the current project and loose its transitive test -scoped dependencies.
- Create a separate project with the test-classes.
The easy way
You can produce a jar which will include your test classes and resources.
. . . . org.apache.maven.plugins maven-jar-plugin 3.3.0 test-jar
To reuse this artifact in an other project, you must declare this dependency with type test-jar :
. . groupId artifactId tests test-jar version test
Based on such configuration there will be two jar files generated. The first one contains the classes from src/main/java whereas the second one will contain the classes from src/test/java . The generated jar files follow the naming schema artifactId-version.jar for the first one and artifactId-version-classifier.jar for the second one. The parts artifactId , versions will be replaced by the values given within your project pom.xml file. The classifier will be set to tests which is a default of the maven-jar-plugin which can be changed if you need by using the configuration in the jar goal.
Note: The downside of this solution is that you don’t get the transitive test -scoped dependencies automatically. Maven only resolves the compile -time dependencies, so you’ll have to add all the other required test -scoped dependencies by hand.
The preferred way
In order to let Maven resolve all test -scoped transitive dependencies you should create a separate project.
groupId artifactId-tests version .
- Move the sources files from src/test/java you want to share from the original project to the src/main/java of this project. The same type of movement counts for the resources as well of course.
- Move the required test -scoped dependencies from the original project to this project and remove the scope (i.e. changing it to the compile -scope). And yes, that means that the junit dependency (or any other testing framework dependency) gets the default scope too. You’ll probably need to add some project specific dependencies as well to let it all compile again.
Now you have your reusable test-classes and you can refer to it as you’re used to:
. . groupId artifactId-tests version test